0

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.

wesside
  • 5,622
  • 5
  • 30
  • 35

3 Answers3

1

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

Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42
1

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.

Community
  • 1
  • 1
Tim Parenti
  • 511
  • 7
  • 26
  • Quite funny, it's exactly the conclusion I came to. Though I have a table in the database that has 336 inputs (every half hour, 7 days a weeks as a time reference.) With a cron that is running every 15 minutes for sending out and another that hits everything 6 hours prior to update the next 6 hour time frames with a TZ convert using the Olson database. Keeps everything on track. – wesside Jan 03 '12 at 03:11
0

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

jakx
  • 748
  • 5
  • 8
  • I just came across this MySQL function for CONVERT_TZ(), where I can convert there time to a GMT standard. Think that will work. – wesside Dec 20 '11 at 17:17
  • 2
    This is the easiest probably the easiest approach, but remember that not all timezones are shifted by an hour, some are half an hour. Not sure if others are different. – loganfsmyth Dec 20 '11 at 17:22