PHP strtotime behavior

The following lines of code return two different outputs on two different servers A and B:

echo date("M Y", strtotime("2015-01"));
echo date("M Y", strtotime("2015-02"));

      

The expected result is "January 2015" and "February 2015", which is correct on server A.

But the same code on server B returns the result as "Jan 2015" and "Mar 2015".

While debugging, I found that the function strtotime

on server B always returns the timestamp for the current day of every month (today is the 29th), which explains why "2015-02" is displayed as "March 2015", (since no February 29, 2015 of the year). Yesterday this code was returning the same result on both servers since Feb 28, and it is correctly translated to Feb 2015.

So on Server A the efficient code is

 echo date("M Y", strtotime("2015-01-01"));
 echo date("M Y", strtotime("2015-02-01"));

      

and on server B - efficient code

echo date("M Y", strtotime("2015-01-29")); //or, strtotime("2015-01-[current day]")
echo date("M Y", strtotime("2015-02-29"));

      

Why is there a difference between these two servers?

+3


source to share


1 answer


This is a problem with a different php version. There is a BC in php 5.2.7, from the documentation :

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given day of the week in a month when that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been fixed in versions 5.2.7 and later.



Demo

Server A has PHP> 5.2.7, server B has PHP <5.2.7.

+2


source







All Articles