dongxian7194 2015-03-21 17:31 采纳率: 0%
浏览 308
已采纳

使用PHP自动将UTC转换为本地时间

Im using MySQL with PHP, and the project is a kind of notification manager

if some one does some thing, a new notification need to be posted in the common post-ground

the issue is - we have users from multiple timezones, currently Im using "datetime" as the field type, so what ever I enter converts into UTC

for example: Im from India(+5:30) and if I create a notification at 21/3/2015 22:50:00, it is saving in DB as 21/3/2015 17:20:00

storing works perfectly well from all zones, but while retrieving it shows for me as 21/3/2015 17:20:00(UTC) but Im expecting in my timezone(IST +5:30) so what Im expecting is 21/3/2015 22:50:00.

is there a way to mention in the query itself to get the time converted in the fetching itself?

or any other way to convert and process further?

any help on this is greatly appreciated.

Thank you

  • 写回答

2条回答 默认 最新

  • duanmo6937 2015-03-21 17:39
    关注

    You can use the function convert_tz so while selecting you can use the following

    mysql> select convert_tz('2015-03-21 17:20:00','+00:00','+05:30') as ist_time;
    +---------------------+
    | ist_time            |
    +---------------------+
    | 2015-03-21 22:50:00 |
    +---------------------+
    1 row in set (0.00 sec)
    

    All you need to do is to change the hard coded date '2015-03-21 17:20:00' to your column name in the selection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络