dongyaoxiu6244 2012-03-11 08:08
浏览 147

PHP将工作日和时间(小时)转换为GMT表示

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

  • 写回答

1条回答 默认 最新

  • dongzi5062 2012-03-11 08:27
    关注

    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');
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮