get_gmt_from_date

函式
get_gmt_from_date ( $string, $format = 'Y-m-d H:i:s' )
引數
  • (string) $string The date to be converted, in the timezone of the site.
    Required:
  • (string) $format The format string for the returned date. Default 'Y-m-d H:i:s'.
    Required:
    Default: 'Y-m-d H:i:s'
返回值
  • (string) Formatted version of the date, in UTC.
定義位置
相關方法
get_date_from_gmtget_post_formatget_the_dateget_comment_dateget_theme_updates
引入
1.2.0
棄用
-

get_gmt_from_date: 這個函式用來將一個給定的日期字串轉換為等效的GMT時間。這對於確保網站的內容在不同的時區顯示一致很有用。

給定一個網站時區的日期,返回該日期的 UTC。

要求並返回一個Y-m-d H:i:s格式的日期。可以使用$format引數重寫返回格式。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
$datetime = date_create( $string, wp_timezone() );
if ( false === $datetime ) {
return gmdate( $format, 0 );
}
return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( $format );
}
function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) { $datetime = date_create( $string, wp_timezone() ); if ( false === $datetime ) { return gmdate( $format, 0 ); } return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( $format ); }
function get_gmt_from_date( $string, $format = 'Y-m-d H:i:s' ) {
	$datetime = date_create( $string, wp_timezone() );

	if ( false === $datetime ) {
		return gmdate( $format, 0 );
	}

	return $datetime->setTimezone( new DateTimeZone( 'UTC' ) )->format( $format );
}

常見問題

FAQs
檢視更多 >