0

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?

Daniel Hutton
  • 1,455
  • 1
  • 17
  • 33
  • I'm not completely following you, but why don't you just store the timestamps in UTC (GMT) format in the DB and convert these to local time based on the timezone of the current user? – wimvds Apr 01 '11 at 13:54
  • This page will only be viewed in our office. What we need is if someone calls from Afghanistan for example, we need to have a piece on the page which says 'GMT time:' and then another piece which says 'Local time:' and gives the time in afghanistan. – Daniel Hutton Apr 01 '11 at 14:01

1 Answers1

0

Using the DateTimeZone is 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.

Jon Nylander
  • 8,743
  • 5
  • 34
  • 45