wesside
wesside

Reputation: 5740

Handing International Time in PHP

Just a quick question on working with International Timezones & GMT.

I have an app that needs to send out an email at a certain time (6am) for every person internationally based on their time/zone GMT +-. I'm at a loss on how this should be handled with GMT & Timezones. Any input is appreciated.

Upvotes: 0

Views: 665

Answers (3)

Tim Parenti
Tim Parenti

Reputation: 511

Naïvely, you could collect (or somehow determine) a time zone ID (tzid) for each of your users, and simply run a cron job at the top of every UTC hour checking if the current time is 06:00 in any of those zones.

It's not that easy, though, because there are several time zones that don't have whole-hour offsets from UTC, such as America/St_Johns (UTC-03:30) and Asia/Kathmandu (UTC+05:45), and these users wouldn't get any emails if you only ran the cron hourly. So you might want to run the cron every quarter-hour just to be on the safe side.

But since cron jobs often start a few seconds after they're scheduled to run, you shouldn't be testing for equality with 06:00:00. Furthermore, time zones don't have to be in quarter-hour increments (but thankfully, all commonly recognized zones are), so to be pedantic, you might want to evaluate whether the current time falls within a certain window.

If, for example, you're running your cron every 15 minutes, make that window 15 minutes wide (e.g., 05:55 to 06:10) to be sure to catch the edge cases. There's still the theoretical chance that a cron run could be skipped... but without storing additional data that indicates whether or not someone has yet been sent today's email (and making up the difference if they haven't), that's about the best you can do.

Upvotes: 1

Manigandan Arjunan
Manigandan Arjunan

Reputation: 2265

You can use the php timezone set function as like below

date_default_timezone_set('America/Los_Angeles');

With the use of this function you can get the time of the user. Based on this you can send the email.

For more referecnce date_default_timezone_set

Upvotes: 1

jakx
jakx

Reputation: 758

You could run a cron to check every hour if one of your user exists somewhere where its 6am.

Upvotes: 0

Related Questions