0

I have a web application that allows users to select to have a certain operation performed at a specific hour on a specific weekday (ex: 7pm Friday) -- recurring every weekday. My users are located in different timezones. Therefore I would like to store the day/hour in one time zone format so that my cron job which runs hourly can query my database and quickly grab all the rows that represent tasks to be performed that hour. What's the best way to accomplish this?

Basically I need to know how to take Friday 7:00pm in Boston, MA or Saturday 3:00pm San Francisco, CA and convert both to GMT (and store in my DB). That way when I run my cron job once an hour I know exactly what value to query the database for given what GMT day/hour it currently is.

EDIT 1 Through playing around I came up with something:

date_default_timezone_set('America/New_York');
$time = strtotime('Friday 8:00pm');

echo date('w : G', $time);
echo '<br>';

date_default_timezone_set('GMT');
$date = date('w : G', $time);

echo $time;
echo '<br>';
echo $date;

The output is:

5 : 20
1331942400
6 : 0

Is this basically the right track?

EDIT 2 Useful detail, I'm actually lucky enough to have my user's timezone information stored as one of these: http://www.php.net/manual/en/timezones.php

Casey Flynn
  • 13,654
  • 23
  • 103
  • 194
  • In case users shall choose time in their timezone, you probably [need to find out their timezone](http://stackoverflow.com/questions/13/how-can-i-determine-a-web-users-time-zone)? The easier possibility would be to just let them choose from GMT and then add a hint with JS with their local time. – aufziehvogel Mar 11 '12 at 08:23
  • @aufziehvogel, I actually already have their timezone. I've played around with an idea and added it as an edit. – Casey Flynn Mar 11 '12 at 08:25
  • Tricky. Maybe a drop down/choice for users to select timezone and then use the date functions in php to rework with the utc offsets, but you also have to allow for daylight saving times. e.g. i am in GMT/UTC 0 but at the end of the month i will be an hour ahead UTC 1 as we swithc to British summertime – PurplePilot Mar 11 '12 at 08:33
  • @PurplePilot, you're right. My solution will be out of sync after a daylight savings time change. – Casey Flynn Mar 11 '12 at 08:36

1 Answers1

2

If you store the users time zone already, you can add the GMT offset to the time to get the GMT date.

I'm located in Colorado which is currently -7 GMT:

$offset = -7;
$gmt = ($offset * 60 * 60) + time();

would give the the unix timestamp for the GMT timezone.

Then make sure your server has GMT as the default or set timezone, you can set the timezone using:

date_default_timezone_set('GMT');
Ben Phelps
  • 196
  • 4
  • 8