mysql_to_rfc3339()
云策文档标注
概述
mysql_to_rfc3339() 函数用于解析 MySQL 日期时间格式(Y-m-d H:i:s)并转换为 ISO8601 格式(Y-m-dTH:i:s)。该函数明确剥离时区信息,因为 MySQL 日期时间不保存时区,以避免误导。
关键要点
- 函数名虽为 mysql_to_rfc3339,但输出不符合 RFC3339 格式(RFC3339 必须包含时区),实际输出为 ISO8601 格式无时区版本。
- 参数 $date_string 为必需,类型为字符串,表示要解析和格式化的日期字符串。
- 返回值为字符串,格式化为 ISO8601 无时区日期时间。
- 内部实现基于 mysql2date() 函数,使用格式 'Y-m-dTH:i:s' 并设置第三个参数为 false 来忽略时区转换。
- 函数自 WordPress 4.4.0 版本引入,常用于 REST API 控制器中处理日期响应。
代码示例
function mysql_to_rfc3339( $date_string ) {
return mysql2date( 'Y-m-dTH:i:s', $date_string, false );
}注意事项
使用时需注意函数名与输出格式的差异,避免在需要 RFC3339 格式(含时区)的场景误用。由于剥离时区,适用于处理 MySQL 存储的无时区日期时间数据。
原文内容
Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601 (Y-m-dTH:i:s).
Description
Explicitly strips timezones, as datetimes are not saved with any timezone information. Including any information on the offset could be misleading.
Despite historical function name, the output does not conform to RFC3339 format, which must contain timezone.
Parameters
$date_stringstringrequired-
Date string to parse and format.
Source
function mysql_to_rfc3339( $date_string ) {
return mysql2date( 'Y-m-dTH:i:s', $date_string, false );
}
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |