drbmhd9583 2016-05-01 11:23
浏览 168
已采纳

将UNIX时间戳转换为本地时间(作为整数)

For a simple PHP graphing script, I need to align time values at local time days. When I do something like $timestamp % 86400 == 0 I get a break at 2 am every day because I'm in UTC +2 here. Also all my grid lines are at 2am, 5am, 8am etc. instead of 0am, 3am, 6am etc.

I understand that UNIX timestamp integer values are always in UTC and that there's date() and gmdate() and such, but I need an integer in local time to do arithmetics on it. How would I do that in PHP? Is there a function such as ToLocalTime() and ToUniversalTime() like in .NET?

To clarify the question, I have nothing to do with readable formatting of the time. No YYYY-MM-DD HH:MM:SS or the like, I just deal with numbers. The input is a number of seconds since epoch in UTC, aka the UNIX timestamp. What I need is a number of the same kind but not in UTC but local time. So I need to convert a UNIX timestamp integer into a local timestamp integer. Hope this is understandable and anybody can imagine what this could be used for (aligning grid lines in a time-axis graph at local time).

  • 写回答

4条回答 默认 最新

  • dongye9191 2016-06-20 19:17
    关注

    I ended up using this to determine the local time UTC offset:

    $tzoffset = date('Z');
    

    Then I just add or subtract that value to timestamps and treat them like UTC times but they are really local times. Technically they are always "off" the real time I intend to work with, but that shift corrects the difference between UTC and local time and lets me do the maths on local times in the "UTC space". When I'm done, I can transform the time back into "local space" for displaying. I could also use the gmdate function and get local time format.

    $now = time() + $tzoffset;
    echo gmdate('...', $now);   // Prints local time
    if ($now % 86400 == 0 && gmdate('w', $now) == 1)
    {
        echo 'Monday midnight local time';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的