php - Removing exactly one month from a date -
it seems can't use strotime
if need reliable , accurate date manipulation. example, if month has 31 days, appears strtotime
minuses 30 days, not whole month.
so, example, if $event["enddate"]
equal "2013-10-31 00:00:01", following code:
echo date("y/n/j", strtotime('-1 month', strtotime($event["enddate"]));
ouputs: 2013/10/1
instead of 2013/09/30
.
question: how know how not it, there another, more accurate, way make php subtract (or add) whole month, , not 30 days?
the main issue 2013/09/31
not exist better approach use first day
or last day
of previous month.
$date = new datetime("2013-10-31 00:00:01"); $date->modify("last day last month"); echo $date->format("y/n/j"); // 2013/9/30
when date 2013-10-15
$date = new datetime("2013-10-15 00:00:01"); $day = $date->format("d"); $year = $date->format("y"); $date->modify("last day last month"); $month = $date->format("m"); if (checkdate($month, $day, $year)) { $date->setdate($year, $month, $day); } echo $date->format("y/n/j"); // 2013-9-15
Comments
Post a Comment