为 MYSQL NOW() 设置时区 [英] Set timezone for MYSQL NOW()

查看:117
本文介绍了为 MYSQL NOW() 设置时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务器上,我保留了一个操作日志,这是使用 MYSQL NOW() 函数生成的时间戳进行跟踪的.但问题是生成的时间戳是 UTC 并且我希望 NOW() 函数为 Asia/Kolkata 生成与我的本地时间相对应的时间戳.我尝试了 SET 时区,但它不起作用

On my server i keep a log of operations and this is tracked using the timestamp generated by MYSQL NOW() function.But the problem is that the timestamp generated is in UTC and i want the NOW() function to generate the timestamp corresponding to my local time for Asia/Kolkata. I tried the SET timezone but it is not working

$queryt="SET time_zone='Asia/Kolkata'";
mysqli_query($mysqli, $queryt))

我做错了什么?

推荐答案

您的问题不清楚,但如果您想将日期和时间值从 UTC 更改为您的时区,您可以使用

Your question is not clear but if you want to change date and time value from UTC to your time zone, you can use

CONVERT_TZ(date, 'UTC',  'Asia/Kolkata')

您可以选择您所在时区的 UTC 日期和时间值:

You can select the UTC date and time value in your time zone:

SELECT CONVERT_TZ(date, 'UTC',  'Asia/Kolkata') AS local FROM table

如果 CONVERT_TZ 函数不起作用,则需要安装时区(时区应该已经存储在 MySQL 中).一个注意事项是您需要将 UTC 值存储在您的数据库中,然后转换为您想要的任何时区.

If CONVERT_TZ function does not work, you need to install time zones (time zones should be already stored in MySQL). One note is that you need to store UTC value in your database and later convert to whatever time zone you want.

这篇关于为 MYSQL NOW() 设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆