Can MySQL convert a stored UTC time to local timezone?(MySQL 可以将存储的 UTC 时间转换为本地时区吗?)
问题描述
MySQL 可以直接在普通的 select 语句中将存储的 UTC 时间转换为本地时区时间吗?
Can MySQL convert a stored UTC time to local time-zoned time directly in a normal select statement?
假设您有一些带有时间戳 (UTC) 的数据.
Let's say you have some data with a timestamp (UTC).
CREATE TABLE `SomeDateTable` (
`id` int(11) NOT NULL auto_increment,
`value` float NOT NULL default '0',
`date` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
)
然后当我
"select value, date from SomeDateTable";
我当然会以存储的 UTC 格式获取所有日期.
I of course get all the dates as in their stored UTC form.
但是假设我想让它们在另一个时区(使用 DST),然后我可以在选择查询中添加一些魔法,以便我在所选时区中获取所有日期吗?
But let's say that I would like to have them in another timezone (with DST), can I then add some magic to the select query so that I get all the dates back in the selected timezone?
"select value, TIMEZONE(date, "Europe/Berlin") from SomeDateTable";
或者我必须在顶部的其他层中执行此操作,例如在某些 php 代码中?(这似乎是大多数人解决这个问题的方法).
Or must I do this in some other layer on top, like in some php code? (it seems to be how most people have solved this problem).
如果您的 MySQL 安装允许您使用 CONVERT_TZ 这是一个非常干净的解决方案,这个例子展示了如何使用它.
If your MySQL installation allows you to use CONVERT_TZ it is a very clean solution, this example shows how to use it.
SELECT CONVERT_TZ( '2010-01-01 12:00', 'UTC', 'Europe/Stockholm' )
但是我不知道这是否是一个好方法,因为某些 MySQL 安装缺少此功能,请谨慎使用.
However I don't know if this is a good way since some MySQL installation is missing this function, use with care.
推荐答案
是的,有 convert_tz 函数.
Yup, there's the convert_tz function.
这篇关于MySQL 可以将存储的 UTC 时间转换为本地时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 可以将存储的 UTC 时间转换为本地时区吗?
基础教程推荐
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
