Timezone translation

Hi folks,

I’m building an app that’s going to be used by people in different countries, therefore different time zones. What I need to do is this, users can login to a room and start a poll. All users cast their votes and when they’re down the poll is over. First of all I need to store the start time and end time of polls in the db table. What datatype should I use?

Secondly, the users need to be able to view the poll results. If I display the poll results including start time and end time, those time stamps will be the server time obviously. But if someone in the UK or Israel is looking at that page, I need the time to be displayed in local browser time, make sense?

Any ideas?

My understanding is that you need to store you dates in UTC time and then when you display the date you use the users time-zone offset. You could ether allow the user to set their own time-zones and store it part of their user account information or try to detect their time-zone, maybe using something like GeoIP. The PHP datetime class could be useful http://www.php.net/manual/en/class.datetime.php

I had a quick look at that class and didn’t see anything blatantly obvious, I’ll have a better look now. I don’t want to have the users do that themselves, there’s an existing app they’re using that does the timezone offset, just can’t get at the source code for that. Thanks for your input.

I did a quick search because I can see this being something I also might need to handle in the future and I found the following links.

Locale-aware Date and Time Formatting in PHP 5.3

PHP DateTime and DateTimeZone Tutorial

http://ditio.net/2008/06/03/php-datetime-and-datetimezone-tutorial/

Localize current time in PHP

It looks fairly simple as long as you know the timezone you want to display.

EG: (taken from ditio tutorial)




$dateTime = new DateTime("now", new DateTimeZone('Europe/Warsaw'));

echo $dateTime->format("Y-m-d H:i:s");

 

$dateTimeZone = new DateTimeZone('GMT');

$dateTime->setTimezone($dateTimeZone);


echo $dateTime->format("Y-m-d H:i:s");



Output:

2008-06-02 21:32:46

2008-06-02 19:32:46

Hope it helps.