iso8601_to_datetime()
iso8601_to_datetime( string $date_string, string $timez…
iso8601_to_datetime( string $date_string, string $timezone = ‘user’ )
将post_date[u gmt]使用的iso8601(Y m dTH:i:sO)日期转换为MySQL DateTime(Y-m-d H:i:s)格式。
Converts an iso8601 (YmdTH:i:sO) date to MySQL DateTime (Y-m-d H:i:s) format used by post_date[_gmt].
目录锚点:#参数#返回#源码
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| $date_string | (string) | 必需 | ISO 8601格式的日期和时间https://en.wikipedia.org/wiki/ISO_。 | 
| $timezone | (string) | 可选 | 如果设置为“gmt”,则返回UTC格式的结果。 | 
返回(Return)
(string|bool)MySQL date time格式的日期和时间-Y-m-d H:i:s,或失败时为false。
源码(Source)
/**
 * Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].
 *
 * @since 1.5.0
 *
 * @param string $date_string Date and time in ISO 8601 format {@link http://en.wikipedia.org/wiki/ISO_8601}.
 * @param string $timezone    Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'.
 * @return string The date and time in MySQL DateTime format - Y-m-d H:i:s.
 */
function iso8601_to_datetime( $date_string, $timezone = 'user' ) {
	$timezone = strtolower($timezone);
	if ($timezone == 'gmt') {
		preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[+|-][0-9]{2,4}){0,1}#', $date_string, $date_bits);
		if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset
			$offset = iso8601_timezone_to_offset($date_bits[7]);
		} else { // we don't have a timezone, so we assume user local timezone (not server's!)
			$offset = HOUR_IN_SECONDS * get_option('gmt_offset');
		}
		$timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]);
		$timestamp -= $offset;
		return gmdate('Y-m-d H:i:s', $timestamp);
	} elseif ($timezone == 'user') {
		return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[+|-][0-9]{2,4}){0,1}#', '$1-$2-$3 $4:$5:$6', $date_string);
	}
}| 更新版本 | 源码位置 | 使用 | 被使用 | 
|---|---|---|---|
| 1.5.0 | wp-includes/formatting.php:3575 | 4 | 1 function | 
类别:WordPress 函数手册、 
		本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。






还没有任何评论,赶紧来占个楼吧!