4

I'm trying the following command in PHP 5.2.12 :

print (date('Y-m-d', strtotime('2009-12 last day')));

Regarding to the php.net manual :

date('m/d/y', strtotime('2009-03 last day')); # 03/31/09

it should display the last day of march 2009 (2009-03-31) !

Mine returns the last day of the previous month ? why ? :

2009-11-30
Kami
  • 5,959
  • 8
  • 38
  • 51

4 Answers4

9

The code you posted fails in the manner you described; it seems that the description in the PHP manual pages (which as mentioned by SilentGhost is just a user comment) is non-verified code.

If you need the last day of a given month, try this:

date('Y-m-d', strtotime("2009-12 next month - 1 hour"));
Roadmaster
  • 5,297
  • 1
  • 23
  • 21
  • 3
    I always use 12 hours, just in case someone sometime decides that daylight savings day is the first or last day of some month. – Milan Babuškov Jul 21 '10 at 21:33
2

If you're using PHP 5.3 try using the DateTime class:

<?php
    $date = new DateTime("2009-03-01");
    $date->modify("last day of previous month");
    echo $date->format("m/d/y");
?>

It must be 5.3 as $date->modify("last day of previous month"); won't work in 5.2.* or earlier.

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

I think you're simply using the relative format incorrectly, this works for me:

echo date('m/d/y', strtotime('last day of 2009-12'));

From the docs:

'last day' ' of'? -- Sets the day to the last day of the current month. This phrase is best used together with a month name following it. -- "last day of next month"

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
0

date("Y-m-t", strtotime("201512"));

For December 2015

tomasbarrios
  • 813
  • 9
  • 15