Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php date give me the same date for 2 different timestamp

Tags:

date

php

here my code :

<?php
    $aaa = array(
        1224972000,
        1224973800,
        1224975600,
        1224977400,
        1224979200,
        1224981000,
        1224982800,
        1224984600,
        1224986400,
        1224988200,
        1224990000,
        1224991800,
        1224993600,
        1224995400,
        1224997200,
    );
    foreach ($aaa as $ts) {
        $date = \date('m/d/Y,H:i:s', $ts);
        echo "$date \n";
    }

And the result is :
10/26/2008,00:00:00
10/26/2008,00:30:00
10/26/2008,01:00:00
10/26/2008,01:30:00
**10/26/2008,02:00:00
10/26/2008,02:30:00
10/26/2008,02:00:00
10/26/2008,02:30:00**
10/26/2008,03:00:00
10/26/2008,03:30:00
10/26/2008,04:00:00
10/26/2008,04:30:00
10/26/2008,05:00:00
10/26/2008,05:30:00
10/26/2008,06:00:00

Why ?

like image 413
jeannot Avatar asked May 01 '26 17:05

jeannot


1 Answers

I'd imagine that in your chosen timezone, the date switches from Daylight Savings Time on that day, so there are two 2:00ams. I see you're studying in Liverpool -- would this be on a British machine? Last Sunday in October is traditionally the shift date between British Summer Time and Greenwich Mean Time. I believe that would also be the case for most of Europe.

Try adding:

    date_default_timezone_set("UTC");

at the start of your script; that will fix the timezone to one without DST adjustments. You should find you get unique results.

like image 68
Matt Gibson Avatar answered May 04 '26 06:05

Matt Gibson