current_time

函式
current_time ( $type, $gmt = 0 )
引數
  • (string) $type Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U', or PHP date format string (e.g. 'Y-m-d').
    Required:
  • (int|bool) $gmt Optional. Whether to use GMT timezone. Default false.
    Required:
返回值
  • (int|string) Integer if `$type` is 'timestamp' or 'U', string otherwise.
定義位置
相關方法
current_datetimecurrent_filtercurrent_actionget_current_themecomment_time
引入
1.0.0
棄用
-

current_time: 這個函式以Unix時間戳的形式返回當前時間,根據WordPress設定中的時區進行調整。它對於顯示WordPress網站上的當前時間或比較日期和時間很有用。

檢索基於指定型別的當前時間。

– mysql”型別將返回MySQL DATETIME欄位格式的時間。
– timestamp’或’U’型別將返回當前時間戳或時間戳和時區偏移的總和,取決於`$gmt’。
– 其他字串將被解釋為PHP日期格式(例如,”Y-m-d”)。

如果`$gmt`是一個整數值,那麼兩種型別都將使用GMT時間,否則輸出將根據網站的GMT偏移量進行調整。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function current_time( $type, $gmt = 0 ) {
// Don't use non-GMT timestamp, unless you know the difference and really need to.
if ( 'timestamp' === $type || 'U' === $type ) {
return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
}
if ( 'mysql' === $type ) {
$type = 'Y-m-d H:i:s';
}
$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
$datetime = new DateTime( 'now', $timezone );
return $datetime->format( $type );
}
function current_time( $type, $gmt = 0 ) { // Don't use non-GMT timestamp, unless you know the difference and really need to. if ( 'timestamp' === $type || 'U' === $type ) { return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); } if ( 'mysql' === $type ) { $type = 'Y-m-d H:i:s'; } $timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone(); $datetime = new DateTime( 'now', $timezone ); return $datetime->format( $type ); }
function current_time( $type, $gmt = 0 ) {
	// Don't use non-GMT timestamp, unless you know the difference and really need to.
	if ( 'timestamp' === $type || 'U' === $type ) {
		return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
	}

	if ( 'mysql' === $type ) {
		$type = 'Y-m-d H:i:s';
	}

	$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
	$datetime = new DateTime( 'now', $timezone );

	return $datetime->format( $type );
}

常見問題

FAQs
檢視更多 >