I'm working with with the facebook API to create a facebook app. When I get info about a user, it tells me the user's timezone:
Array
(
[id] => 1234567890
[name] => Hugh Jass
[first_name] => Hugh
[last_name] => Jass
[link] => http://www.facebook.com/profile.php?id=1234567890
[timezone] => -7
[updated_time] => 2010-05-30T00:14:06+0000
)
When the user uses my app, my server can record a timestamp of any particular action or event. I'd like to know the most robust/elegant way to convert these timestamps to something meaningful to a local user. Nobody wants Greenwich time unless you live in Greenwich. My Los Angeles users want Pacific Time or whatever (which varies with daylight savings time).
Am I right in understanding that the [man]time[/man] function returns the current UTC/GMT timestamp? Am I also right in understanding that it is the [man]date[/man] function which converts that time stamp to the local time zone whereas gmdate simply converts a timestamp? This code seems to confirm that:
$now = time();
echo date('Y-m-d H:i:sP', $now); // 2010-06-02 18:08:38-05:00
echo gmdate('Y-m-d H:i:sP', $now); // 2010-06-02 23:08:38+00:00
// if you change the default timezone, they match:
// change timezone
date_default_timezone_set('UTC');
$now = time();
echo date('Y-m-d H:i:sP', $now); // 2010-06-02 23:12:01+00:00
echo gmdate('Y-m-d H:i:sP', $now); // 2010-06-02 23:12:01+00:00
I'm sort of puzzled about how to handle these timestamps which will be stored in a db due to the implicit time-zone related conversions that happen. I'm guessing that storing events with UTC timestamps is the way to go so I can handle timezone conversions more smoothly.
I expect I'll use datetime fields in the db. I will NOT use mysql's NOW function or CURRENT_TIMESTAMP or whatever. To capture the current UTC time in YYYY-MM-DD HH:MM:SS format would be done thusly:
$UTC_datetime = gmdate('Y-m-d H:i:s');
To display the current time in the user's timezone seems a bit trickier. If any has hints about how to do this better, I would much appreciate it:
// let's assume $event_datetime was the UTC datetime when some event happened
$event_datetime = '2010-04-30 08:30:00';
// the user's timezone as reported from the FB api
$user_timezone = -7;
// does this work?
$local_timestamp = strtotime($event_datetime . ' ' . $user_timezone . ' hours');
This seems convoluted to me. More importantly, I'm wondering how it might be impacted by daylight savings.
If anyone has a better way or any insights, I'd love to hear about it.