Developing apps is great until I have to start dealing with blasted timezones. For some reason this aspect of it always does my head in.
At the moment I'm storing all dates in the DB as GMT with the format Y-m-d H:i:s. I figure it's good to be consistent and store dates in GMT then convert to the users desired timezone. So I allow users to choose there timezone, based off the defined PHP timezones that can be used with the DateTimeZone class.
My problem, however, arises with the whole syncing of times. Let's say I post something right now. The date gets stored in in the database as: 2012-04-14 02:35:49
Now I'm in GMT +10 (possibly the greatest of timezones ), so I've set my timezone in my settings as Australia/Melbourne and when displaying date/times locally I do something like this:
public static function local($user, $date, $format = 'F j, Y, h:ia')
{
$date = new DateTime($date);
return $date->setTimezone(new DateTimeZone($user->timezone))->format($format);
}
So running the stored date above through my method gives me a date like this: April 14, 2012, 12:35pm
Awesome.
My question is: I noticed that on say, Twitter, when you change your timezone your tweet dates don't change. They remain as the date/time of my actual timezone, regardless of what I set it too. With my implementation however, if I change my timezone to GMT +12 (Fiji) my post is now posted at: April 14, 2012, 02:35pm
So is what I'm doing right? My head has about had it.
Thanks.