rest_get_date_with_gmt

函数
rest_get_date_with_gmt ( $date, $is_utc = false )
参数
  • (string) $date RFC3339 timestamp.
    Required:
  • (bool) $is_utc Whether the provided date should be interpreted as UTC. Default false.
    Required:
    Default: false
返回值
  • (array|null) Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s), null on failure.
相关
  • rest_parse_date()
定义位置
相关方法
get_date_from_gmtrest_get_route_for_termrest_get_route_for_postget_date_templaterest_get_avatar_sizes
引入
4.4.0
弃用
-

rest_get_date_with_gmt:该函数用于检索GMT时区的日期字符串。它接受一个单一的参数,一个日期字符串,并返回在GMT时区的同一日期。它在REST API中用于格式化日期字符串。

将一个日期解析为本地和UTC的等价物,以MySQL日期时间格式。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rest_get_date_with_gmt( $date, $is_utc = false ) {
/*
* Whether or not the original date actually has a timezone string
* changes the way we need to do timezone conversion.
* Store this info before parsing the date, and use it later.
*/
$has_timezone = preg_match( '#(Z|[+-]d{2}(:d{2})?)$#', $date );
$date = rest_parse_date( $date );
if ( empty( $date ) ) {
return null;
}
/*
* At this point $date could either be a local date (if we were passed
* a *local* date without a timezone offset) or a UTC date (otherwise).
* Timezone conversion needs to be handled differently between these two cases.
*/
if ( ! $is_utc && ! $has_timezone ) {
$local = gmdate( 'Y-m-d H:i:s', $date );
$utc = get_gmt_from_date( $local );
} else {
$utc = gmdate( 'Y-m-d H:i:s', $date );
$local = get_date_from_gmt( $utc );
}
return array( $local, $utc );
}
function rest_get_date_with_gmt( $date, $is_utc = false ) { /* * Whether or not the original date actually has a timezone string * changes the way we need to do timezone conversion. * Store this info before parsing the date, and use it later. */ $has_timezone = preg_match( '#(Z|[+-]d{2}(:d{2})?)$#', $date ); $date = rest_parse_date( $date ); if ( empty( $date ) ) { return null; } /* * At this point $date could either be a local date (if we were passed * a *local* date without a timezone offset) or a UTC date (otherwise). * Timezone conversion needs to be handled differently between these two cases. */ if ( ! $is_utc && ! $has_timezone ) { $local = gmdate( 'Y-m-d H:i:s', $date ); $utc = get_gmt_from_date( $local ); } else { $utc = gmdate( 'Y-m-d H:i:s', $date ); $local = get_date_from_gmt( $utc ); } return array( $local, $utc ); }
function rest_get_date_with_gmt( $date, $is_utc = false ) {
	/*
	 * Whether or not the original date actually has a timezone string
	 * changes the way we need to do timezone conversion.
	 * Store this info before parsing the date, and use it later.
	 */
	$has_timezone = preg_match( '#(Z|[+-]d{2}(:d{2})?)$#', $date );

	$date = rest_parse_date( $date );

	if ( empty( $date ) ) {
		return null;
	}

	/*
	 * At this point $date could either be a local date (if we were passed
	 * a *local* date without a timezone offset) or a UTC date (otherwise).
	 * Timezone conversion needs to be handled differently between these two cases.
	 */
	if ( ! $is_utc && ! $has_timezone ) {
		$local = gmdate( 'Y-m-d H:i:s', $date );
		$utc   = get_gmt_from_date( $local );
	} else {
		$utc   = gmdate( 'Y-m-d H:i:s', $date );
		$local = get_date_from_gmt( $utc );
	}

	return array( $local, $utc );
}

常见问题

FAQs
查看更多 >