rest_parse_date

函式
rest_parse_date ( $date, $force_utc = false )
引數
  • (string) $date RFC3339 timestamp.
    Required:
  • (bool) $force_utc Optional. Whether to force UTC timezone instead of using the timestamp's timezone. Default false.
    Required:
    Default: false
返回值
  • (int) Unix timestamp.
定義位置
相關方法
rest_parse_hex_colorget_the_dateget_core_updateslist_core_updaterest_parse_request_arg
引入
4.4.0
棄用
-

rest_parse_date:這是一個WordPress的函式,它解析一個日期字串並返回一個相應的Unix時間戳: 該函式接收一個日期字串和一個格式引數,並返回相應的Unix時間戳: 這個函式用來解析傳遞給WordPress REST API的日期字串。

將RFC3339時間解析為Unix時間戳。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rest_parse_date( $date, $force_utc = false ) {
if ( $force_utc ) {
$date = preg_replace( '/[+-]d+:?d+$/', '+00:00', $date );
}
$regex = '#^d{4}-d{2}-d{2}[Tt ]d{2}:d{2}:d{2}(?:.d+)?(?:Z|[+-]d{2}(?::d{2})?)?$#';
if ( ! preg_match( $regex, $date, $matches ) ) {
return false;
}
return strtotime( $date );
}
function rest_parse_date( $date, $force_utc = false ) { if ( $force_utc ) { $date = preg_replace( '/[+-]d+:?d+$/', '+00:00', $date ); } $regex = '#^d{4}-d{2}-d{2}[Tt ]d{2}:d{2}:d{2}(?:.d+)?(?:Z|[+-]d{2}(?::d{2})?)?$#'; if ( ! preg_match( $regex, $date, $matches ) ) { return false; } return strtotime( $date ); }
function rest_parse_date( $date, $force_utc = false ) {
	if ( $force_utc ) {
		$date = preg_replace( '/[+-]d+:?d+$/', '+00:00', $date );
	}

	$regex = '#^d{4}-d{2}-d{2}[Tt ]d{2}:d{2}:d{2}(?:.d+)?(?:Z|[+-]d{2}(?::d{2})?)?$#';

	if ( ! preg_match( $regex, $date, $matches ) ) {
		return false;
	}

	return strtotime( $date );
}

常見問題

FAQs
檢視更多 >