Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP date() losing an hour from UNIX Epoch

Tags:

date

php

datetime

Hi I am experiencing some strange behavior with the php date() function. I am trying to increment dates by a week at a time like so.

CODE:

<?php
    date_default_timezone_set('Europe/London');
    echo 7*24*60*60;
    echo '<br>';
    echo date('d/m/Y H:i:s', 0);
    echo '<br>';
    echo date('d/m/Y H:i:s', 604800);
?>

Which results in the following output.

OUTPUT:

604800
01/01/1970 01:00:00
08/01/1970 01:00:00

Which is what you would expect, the date has incremented by 7 days to the second. But after I reach a certain amount of seconds the date function seems to loose an hour off the date.

CODE:

<?php
    date_default_timezone_set('Europe/London');
    echo (1351468800 - 1350864000);
    echo '<br>';
    echo date('d/m/Y H:i:s', 1350864000);
    echo '<br>';
    echo date('d/m/Y H:i:s', 1351468800);
?>

Results in the following output

OUTPUT:

604800
22/10/2012 01:00:00
29/10/2012 00:00:00

As you can see the date has lost an hour even though the difference between the two dates is 604800 seconds. I have tested this on two different servers and I have also tested similar code using the DateTime object but still the same result. Where am I going wrong?

like image 581
Theo Kouzelis Avatar asked Jan 16 '23 11:01

Theo Kouzelis


2 Answers

I believe the DST is coming into play here. As DST ends on 28th of Oct 2012 in London.

like image 50
Sabeen Malik Avatar answered Jan 18 '23 01:01

Sabeen Malik


Use strtotime more reliable

date_default_timezone_set('Europe/London');
$startDate  = "1350864000" ;
$senvenDays = strtotime("+7 day", $startDate);
var_dump(date("d/m/Y H:i:s",$startDate));
var_dump(date("d/m/Y H:i:s",$senvenDays));
var_dump($senvenDays - $startDate);

Output

string '22/10/2012 01:00:00' (length=19)
string '29/10/2012 01:00:00' (length=19)
int 608400
like image 41
Baba Avatar answered Jan 18 '23 00:01

Baba