Php 如何在MySQL数据库中存储UTC ISO8601日期?
我有以下格式的数千个日期:Php 如何在MySQL数据库中存储UTC ISO8601日期?,php,mysql,date,date-format,iso8601,Php,Mysql,Date,Date Format,Iso8601,我有以下格式的数千个日期: 2011-10-02T23:25:42Z(UTC中又称ISO 8601) 我应该使用什么MySQL数据类型在MySQL数据库中存储这样的ISO8601日期?例如,日期时间,时间戳或其他什么 哪种方法最适合比较(例如,获取两个日期/时间之间的记录)和对查询结果进行排序?如果数据库非常大怎么办 将上述PHP字符串转换为MySQL存储的最佳方法是什么?(我猜应该使用date\u default\u timezone\u set('UTC');)以下是使用datetime更好
2011-10-02T23:25:42Z
(UTC中又称ISO 8601)
我应该使用什么MySQL数据类型在MySQL数据库中存储这样的ISO8601日期?例如,日期时间
,时间戳
或其他什么
哪种方法最适合比较(例如,获取两个日期/时间之间的记录)和对查询结果进行排序?如果数据库非常大怎么办
将上述PHP字符串转换为MySQL存储的最佳方法是什么?(我猜应该使用
date\u default\u timezone\u set('UTC');
)以下是使用datetime更好的原因
您可以使用
DateTime
数据类型来存储日期和时间
使用CAST
函数将这些字符串转换为mysqlDateTime
类型
以下是一个例子:
CAST("2011-10-02T23:25:42Z" AS DATETIME)
这将为您提供2011-10-02 23:25:42
希望这能对您有所帮助。我认为将日期时间值保存在
DATETIME
类型的字段中是一种自然的方式
根据我目前使用PHP应用程序的经验,只有与此信息相关的read
/write
操作可能有问题
正确执行整个过程的一种可能的解决方案(假设您使用DATETIME
数据类型)可以是以下方法:
读取用于PHP的日期时间值
DATETIME
字段,将查询中的字段转换为'2011-10-02T23:25:42Z'
格式的字符串表示形式,方法是使用DATE\u格式
MySQL函数和'%Y-%m-%dT%H:%i:%sZ'
格式化字符串()DateTime
类对象和DateTime::createFromFormat
给定的静态方法'Y-m-d\TH:i:s\Z'
格式化字符串(T
和Z
被转义,以避免将它们视为格式化指令)()DateTime
class对象的format
方法,使用与前面相同的'Y-m-d\TH:i:s\Z'
格式化字符串(),将PHPDateTime
类对象转换为UTC格式字符串表示的ISO 8601INSERT
/UPDATE
操作STR\u TO\u DATE
(使用'%Y-%m-%dT%H:%i:%sZ'
格式化字符串),将其转换为真实的数据库DATETIME
值()$db = new PDO('mysql:host=localhost;dbname=my_db;charset=utf8', 'username', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
// run the query aquring 1 example row with DATETIME data
// converted with MySQL DATE_FORMAT function to its string representation
// in the chosen format (in our case: ISO 8601 / UTC)
$stmt = $db->query("SELECT DATE_FORMAT(dt_column, '%Y-%m-%dT%H:%i:%sZ') AS formatted_dt_col"
." FROM your_table LIMIT 1");
if($stmt !== FALSE) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// convert the acquired string representation from DB
// (i.e. '2011-10-02T23:25:42Z' )
// to PHP DateTime object which has all the logic of date-time manipulation:
$dateTimeObject = DateTime::createFromFormat('Y-m-d\TH:i:s\Z', $row['formatted_dt_col']);
// the following should print i.e. 2011-10-02T23:25:42Z
echo $dateTimeObject->format('Y-m-d\TH:i:s\Z');
// now let's write PHP DateTime class object '$dateTimeObject'
// back to the database
$stmtInsertDT = $db->prepare("INSERT INTO your_table(dt_column) "
. " VALUES ( STR_TO_DATE(:par_formatted_dt_column, '%Y-%m-%dT%H:%i:%sZ') )");
$dtAsTextForInsert = $dateTimeObject->format('Y-m-d\TH:i:s\Z');
// convert '$dateTimeObject' to its ISO 8601 / UTC text represantation
// in order to be able to put in in the query using PDO text parameter
$stmtInsertDT->bindParam(':par_formatted_dt_column', $dtAsTextForInsert, PDO::PARAM_STR);
$stmtInsertDT->execute();
// So the real insert query being perform would be i.e.:
/*
INSERT INTO your_table(dt_column)
VALUES ( STR_TO_DATE('2011-10-02T23:25:42Z', '%Y-%m-%dT%H:%i:%sZ') )
*/
}
}
catch(\PDOException $pexc) {
// serve PDOException
}
catch(\Exception $exc) {
// in case of no-PDOException, serve general exception
}
这种方法在PHP和MySQL数据库之间操作日期时间值方面帮助了我很多
我希望它也能对您有所帮助。您可以使用
php
的strotime
功能轻松转换日期:
date_default_timezone_set('UTC');
$date = '2011-10-02T23:25:42Z';//(aka ISO 8601 in UTC)
$time = strtotime($date); //time is now equals to the timestamp
$converted = date('l, F jS Y \a\t g:ia', $time); //convert to date if you prefer, credit to Marc B for the parameters
现在,您只需使用timestamp
或datetime
在MySQL
中插入日期,这取决于哪种类型最适合您的需要。下面是关于这两种类型的最重要的内容
时间戳
- 协调世界时“1970-01-01 00:00:01”至“2038-01-09 03:14:07”的范围
- 受时区设置的影响
- 4字节存储
- 允许在所有版本的列上更新当前时间戳时使用
- 索引要快得多
不是可能的默认值NULL
- 将值从当前时区转换为
进行存储,并从UTC
转换回当前时区进行检索UTC
Datetime
- 范围为“1000-01-01 00:00:00”到“9999-12-31 23:59:59”
- 常数(时区不会影响)
- 8字节存储
- 仅允许从版本
5.6.5
timestamp
,因为存储更小,索引速度更快,这将为你提供更好的比较性能。但是,你必须确保你的日期符合我前面提到的timestamp
的限制se您别无选择,必须使用datetime
标准时间的文档:
为了让SO的回答者每天不断重复不要使用mysql*
不推荐的函数,请在插入时使用PDO
或mysqli*
您不能以原始UTC ISO8601格式(使用
2011-10-02T23:25:42Z
表示)存储日期并保存所有SQL日期时间功能
但是您应该知道,MySQL(关于)总是以UTC存储时间/日期。
您还可以修改连接的时区
所以,如果用PHP执行
date_default_timezone_set('UTC');
在MySQL中
SET time_zone = +00:00
当然PHP和MySQL会使用UTC
之后,您可以将所有数据库字符串转换为DateTime,而不必考虑时区不匹配
要转换任何PHP Da
$datetime->setTimezone(new DateTimeZone('UTC'))->format('Y-m-d H:i:s');
SELECT CONVERT_TZ(str_to_date('2011-10-02T23:25:42Z','%Y-%m-%dT%H:%i:%sZ'),'+00:00','SYSTEM') from dual;
2011-10-02 16:25:42
SELECT CONVERT_TZ(str_to_date('2011-10-02T23:25:42.123456Z','%Y-%m-%dT%H:%i:%s.%fZ'),'+00:00','SYSTEM') from dual;
2011-10-02 16:25:42.123456