Idi Idi - 1 year ago 102
PHP Question

Is this a PHP bug on date?

Doing this on PHP 5.6.14:

$a = strtotime('2016-10-01');
$b = $a+29*24*3600;
$c = $a+30*24*3600;
die(date("d/m/Y",$b).' - '.date("d/m/Y",$c));

will return
30/10/2016 - 30/10/2016

How strange since I expect
to be
instead of
. Is it a PHP bug?

Notes: I know there are several ways to do date operations, but I'm asking specifically on this. If I change the
month to other numbers than 10 (e.g. 8,9,11,12) it gave me expected results.

Answer Source

The way to fix this issue is by adding midday like so:

$a = strtotime('2016-10-01 12:00');
$b = $a+29*24*3600;
$c = $a+30*24*3600;
die(date("d/m/Y",$b).' - '.date("d/m/Y",$c));

The reason why is because you did not include a time so PHP assumed "now" for the time part of strtotime. You then have day light savings in October in your country (well, the country of the server) and this pulled back the closing time value of $c.

Adding midday (12:00) gives you a buffer. It does not need to be 12:00 as 1:00 would have worked as well but personally I like to work with 12:00.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download