phptimetimezoneunix-timestampiana

How to get the time hh:mm:ss of any state starting by seconds from midnight without changing date_default_timezone_set regardless of the state I am in


For some reason in my code at the top of the page there's date_default_timezone_set('America/Belize'); and I don't want to change it.

For getting the current London seconds from midnight (or any other timezone) regardless of the state I am in I've tryied with a small function (just a little modded) found here on StackOverflow:

function seconds_from_midnight($iana_timezone = 'Europe/London'){
    $time_now = time(); //UNIX TIMESTAMP (IT'S THE SAME EVERYWHERE)
    $date = new DateTime(); 
    $date->setTimestamp( $time_now );
    $date->setTimezone(new DateTimeZone($iana_timezone));
    $date->modify('today midnight');
    $midnight_time = $date->getTimestamp();
    $seconds_since_midnight = $time_now - $midnight_time;
    return $seconds_since_midnight;
}

I think this first part it's correct because it use a unix timestamp (UTC-0 starting from 1970).

Then I check the time in London with (this part is completely wrong):

$r = seconds_from_midnight();
echo 'Time London now: '.date('H:i:s', $r).'<br>'; //How to do this for each state?

[It should be the correct time if you are a person in London looking at the clock right now]

Now see the problem that occurs:

date it's refers to the server, so gives me back a wrong time (I don't know why but I think the reason it's date_default_timezone_set at the beginning of the page, that I don't want to change).

Please note: The timezone given by the variable $iana_timezone may be different from Europe/London.

I don't live in America/Belize and the PHP server it is in an other state (I don't know which one and honestly I don't care).

So, how to get the time from midnight in London (or in another state) in this conditions?


Solution

  • Your function returns the correct result, but your indirect check is not quite correct.

    date('H:i:s', $r) does not contain time zone information, so it uses default time zone (America/Belize). But, to begin with, you're using a relative amount of seconds as an absolute Unix time. You are basically taking Unix Epoch as starting point, extracting a time from 1st January 1970 and printing it in current default time zone. Unix Epoch in Belize didn't happen even near midnight, so the time printed is way off track.

    Even if you change default time zone temporarily for this task, London is currently using daylight saving time, so it'll be 1 hour off. It'll work by pure chance after last Sunday in October and will break again in March.

    If you need to handle different time zones, use DateTime all the way through.

    function seconds_from_midnight(string $iana_timezone = 'Europe/London'): int
    {
        $timeZone = new DateTimeZone($iana_timezone);
        $now = new DateTime();
        $midnight = new DateTime('today midnight', $timeZone);
        return $now->getTimestamp() - $midnight->getTimestamp();
    }
    
    $seconds = seconds_from_midnight();
    
    $londonTimeZone = new DateTimeZone('Europe/London');
    $midnightInLondon = new DateTimeImmutable('today midnight', $londonTimeZone);
    $nowInLondon = new DateTimeImmutable(timezone: $londonTimeZone);
    $sum = $midnightInLondon->modify("+$seconds seconds");
    
    var_dump($seconds, $midnightInLondon, $nowInLondon, $sum);