Theo Kouzelis
Theo Kouzelis

Reputation: 3523

PHP date() losing an hour from UNIX Epoch

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?

Upvotes: 3

Views: 336

Answers (2)

Baba
Baba

Reputation: 95141

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

Upvotes: 3

Sabeen Malik
Sabeen Malik

Reputation: 10880

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

Upvotes: 5

Related Questions