douweiluo0600 2016-09-18 09:58
浏览 177
已采纳

从GMT时间和用户时区计算时间

I have one mysql DB saving time for every record with GMT time for every user. like

id user_id   date_added
1  1        2016-06-08 12:22:19
2  2        2016-06-08 11:22:14
3  4        2016-06-08 11:02:09
4  1        2016-06-08 10:12:11

I also have table saved timezone for each user like

id  user_name   timezone
1   user1       Pacific/Midway
2   user2       Asia/Dubai

now i want to calculate real time for user record using above time zone in PHP.

i tried many examples but not working cause some zone are + and some are -

Thanks

  • 写回答

3条回答 默认 最新

  • dth20986 2016-09-18 10:35
    关注

    Assuming that all dates are stored in UTC date format, you need to convert the timezone to get the local time for each user.

    Example:

    // default timezone is UTC
    $date = new DateTime('2016-06-08 12:22:19', new DateTimeZone('UTC'));
    var_dump($date->format('Y-m-d H:i:sP'));
    
    // convert timezone to Asia/Dubai +4 UTC
    $date->setTimezone(new DateTimeZone('Asia/Dubai'));
    var_dump($date->format('Y-m-d H:i:sP'));
    

    The output will be

       string '2016-06-08 12:22:19+00:00' (length=25) 
       string '2016-06-08 16:22:19+04:00' (length=25)
    

    To get rid of +00 and +04 change the format by removing P

    $date->format('Y-m-d H:i:s')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?