dongzhong7443 2011-08-31 00:49
浏览 92

PHP同步时区夏令时问题

I have a website that loads data into a MySQL table. Two of the entries in the table are 'startdate' and 'enddate'. 'startdate' is automatic using CURRENT_TIMESTAMP. 'enddate' is chosen by the user using a date picker.

This creates a problem if the user is in a different timezone than the server. For example (I'm in Pacific timezone, server is in Central). If I create an entry at 5pm and choose an end date of 10pm, it goes into the database as 7pm and 10pm, creating a 3-hour window instead of 5. What I want is to convert the end date to Central time (the same as the server). So, using my example, I would want the entries in the database to be 7pm and 12 midnight.

I can get the users timezone offset with this javascript:

var d = new Date();
var timezone = -d.getTimezoneOffset()/60;

This returns -7 (I'm in GMT-8, I'm assuming the 1-hour dif is because of daylight savings). My first thought for a solution is to assume the server is in -5, and compare the two (going back to my example, -5 - -7 = 2, so add 2 to the 'enddate').

The problem comes when we switch back to standard time. I assume that my javascript will start to return -8 instead of -7, breaking my function. I know about PHP's date_default_timezone_get(), but that returns a string instead of a number. I guess what I need is a similar function that would (for Central time) return -5 during daylight savings and -6 during standard time.

  • 写回答

2条回答 默认 最新

  • dongxian7194 2011-08-31 01:01
    关注

    Use a mysql timestamp field.

    Or save all the data in UTC, and then do what needs to be done after in php.

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线