Php: strtotime ("12/31/2004 +6 months")); not returning on the last day of June

I expected this functionality to return 6/30/2005instead 7/1/2005.

print date("m/d/Y", strtotime("12/31/2004 +6 month"));

Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month"))returns 03/03/2011as long as he wants to return 2/28/2011.

Does anyone know if there is a direct way to show the last day of the month?

+5
source share
4 answers

How about this one?

echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011

Demo

Change: . For reference, a link to the documentation for relative times .

+6
source

since strtotime lasts until the next month, if there are no days in that month, you can go back for 6 months and check if it ends at the start date.

  $date2 = date("Y-m-d", strtotime("{$date} +6 months"));
  $date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
  if($date3 != $date)
  {
     $date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
  }

( "m/t/Y" )

+2

, , , day . , mktime()

$mymonth = 2;   // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));

2/28/2011.

0

strtotime .

1/31/2011 +1month

2/31/2011

28 ( 29) . 2011 , "31 " "3 ".

'12/31/2004 + 6month '. 31 2005 . 30 , 1 .

0

All Articles