Timezones in Laravel 4, part two

(Note – this isn’t a proper blog post, it is just a dot-point list of links for my own reference. Read and/or use it at your own risk.)

A lot of people suggest saving the user’s timezone string in the User model, so dates / times stored in UTC can be displayed in the timezone for that user.

But how to get the user’s timezone string?

I could auto-detect it in Javascript using jsTimezoneDetect (approximate only)

or I can get it from Google Time Zone API, but only if I already have latitude / longitude

To get the lat/long, either:

1 – Get from the user using HTML5 location, either via raw Javascript or via jQuery … though the user needs to give permission, and I’d want to explain why first.

2 – Get location from the IP address using IPInfoDB (free but requires registration and only works from one server IP) or freegeoip.net (no registration required but stops after 10k queries p/h) or geoPlugin (requests registration but seems to work without it).

And once I’ve auto-detected the timezone string, I’d then need to give the user a chance to change it in a settings page, because the auto-detected value will sometimes be wrong.

…or I could just send everything to the browser in UTC and let Javascript convert it using the browser’s timezone info. Heck, that might be easier…

…though I don’t think I need to do any of that. I’m using Facebook for my only login path, and I can get the current timezone offset from the Facebook User object. (Not the timezone string, just the offset … but that’s probably good enough, provided Fb keep it up-to-date, which they do.) No user interaction required, and it’s reliable (or at least it’s a close to reliable as Facebook is). My guess is that Fb gets it from Date().getTimezoneOffset() each time a session starts.

Timezones in Laravel 4, part two

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s