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:
$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?