iso8601_to_datetime

函数


iso8601_to_datetime ( $date_string, $timezone = 'user' )
参数
  • (string)
    $date_string
    Date and time in ISO 8601 format {@link}.
    Required:
  • (string)
    $timezone
    Optional. If set to ‘gmt’ returns the result in UTC. Default ‘user’.
    Required:
    Default: ‘user’
返回值
  • (string|false) The date and time in MySQL DateTime format – Y-m-d H:i:s, or false on failure.
定义位置
  • wp-includes/formatting.php
    , line 3653
引入
1.5.0
弃用

给出一个ISO 8601(YmdTH:i:sO)日期,返回post_date[_gmt]使用的MySQL DateTime(Y-m-d H:i:s)格式。

function iso8601_to_datetime( $date_string, $timezone = 'user' ) {
	$timezone    = strtolower( $timezone );
	$wp_timezone = wp_timezone();
	$datetime    = date_create( $date_string, $wp_timezone ); // Timezone is ignored if input has one.

	if ( false === $datetime ) {
		return false;
	}

	if ( 'gmt' === $timezone ) {
		return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d H:i:s' );
	}

	if ( 'user' === $timezone ) {
		return $datetime->setTimezone( $wp_timezone )->format( 'Y-m-d H:i:s' );
	}

	return false;
}