I want to make a system which displays time dependant on the timezone in a particular country. E.g. a table in my database contains country names in one field, and in the column next to it, it will contain GMT-compared timezones like -0100 or -0200.
What I need to do on the webpage then is set current GMT time ($time), and then pull the information from the database and alter $time to be GMT -1 or GMT -2.
Could someone tell me what the easiest way to do this would be please?
Thanks for any help
Edit:
So far I have this code:
<?php
$time = date("H:i:s", time());
echo $time;
echo "<br/><br/>";
$myDateTime = new DateTime($time, new DateTimeZone('GMT'));
$myDateTime->setTimezone(new DateTimeZone('Antarctica/South_Pole'));
echo $myDateTime->format('Y-m-d H:i');
?>
which seems to do the job, and I could enter into the database ‘Antarctica/South_Pole’ rather than the GMT-/+ time. Would this do the job OK in the long run?
Also, using this method, would Daylight saving times be worked out OK?
Using the
DateTimeZoneis the correct way to go about it.If you provide it with zoneinfo keys (such as America/New_York, or Antarctica/South_Pole as in your example) Daylight Saving time WILL be factored into the result, which is not the case if you have a naive database with only the UTC offset stored.