douweiluo0600 2016-09-18 09:58 采纳率: 0%
浏览 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条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动