Jump to content

Timezone Syncing


JasonLewis

Recommended Posts

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.

 

 

Link to comment
Share on other sites

I've come to the conclusion that Twitter most likely uses JavaScript to update their dates. You can check this by changing your computers date/time and refreshing the page.

As an educated guess I'd say Twitter keeps the timezone setting as a server-side fall back in case something goes wrong client-side. Fair enough. I've gone the same way.

 

I'm using PHPDate for jQuery to make formatting my dates easier and also using this conversion script found on StackOverflow.

 

This has turned into a client-side thing now. Ah well, hopefully this can help someone.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.