PHP strtotime (YYYY-mm last day) with a month error

I am trying to execute the following command in PHP 5.2.12:

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

Regarding 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? What for?

 2009-11-30 
+4
source share
4 answers

The code you posted does not match the method you described; it seems that the description on the PHP manual pages (which, as mentioned in SilentGhost, is just a user comment), is unconfirmed code.

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

 date('Ym-d', strtotime("2009-12 next month - 1 hour")); 
+9
source

If you are 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 should be 5.3 since $date->modify("last day of previous month"); will not work in 5.2. * or earlier.

+2
source

I think you are 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 until the last day of the current month. This phrase is best used with the name of the month following it. - "last day of next month"

0
source

date("Ymt", strtotime("201512"));

In December 2015

0
source

Source: https://habr.com/ru/post/1305027/


All Articles