wp_resolve_post_date

函式
wp_resolve_post_date ( $post_date = '', $post_date_gmt = '' )
引數
  • (string) $post_date The date in mysql format.
    Required:
    Default: (empty)
  • (string) $post_date_gmt The GMT date in mysql format.
    Required:
    Default: (empty)
返回值
  • (string|false) A valid Gregorian-calendar date string, or false on failure.
定義位置
相關方法
wp_reset_postdatawp_set_post_cats_wp_translate_postdatawp_remote_postwp_create_post_autosave
引入
5.7.0
棄用
-

wp_resolve_post_date: 這個函式解決了文章釋出日期和修訂之間的衝突。它用於確保在顯示修訂時使用正確的文章日期。

使用wp_checkdate來為post_date返回一個有效的公曆值。
如果沒有提供post_date,這將首先檢查post_date_gmt(如果提供了),然後返回到使用當前時間。

在wp_insert_post中,一個空的post_date和一個無效的post_date_gmt將繼續返回’1970-01-01 00:00:00’而不是false。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
// If the date is empty, set the date to now.
if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
$post_date = current_time( 'mysql' );
} else {
$post_date = get_date_from_gmt( $post_date_gmt );
}
}
// Validate the date.
$month = (int) substr( $post_date, 5, 2 );
$day = (int) substr( $post_date, 8, 2 );
$year = (int) substr( $post_date, 0, 4 );
$valid_date = wp_checkdate( $month, $day, $year, $post_date );
if ( ! $valid_date ) {
return false;
}
return $post_date;
}
function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { // If the date is empty, set the date to now. if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) { if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) { $post_date = current_time( 'mysql' ); } else { $post_date = get_date_from_gmt( $post_date_gmt ); } } // Validate the date. $month = (int) substr( $post_date, 5, 2 ); $day = (int) substr( $post_date, 8, 2 ); $year = (int) substr( $post_date, 0, 4 ); $valid_date = wp_checkdate( $month, $day, $year, $post_date ); if ( ! $valid_date ) { return false; } return $post_date; }
function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
	// If the date is empty, set the date to now.
	if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
		if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
			$post_date = current_time( 'mysql' );
		} else {
			$post_date = get_date_from_gmt( $post_date_gmt );
		}
	}

	// Validate the date.
	$month = (int) substr( $post_date, 5, 2 );
	$day   = (int) substr( $post_date, 8, 2 );
	$year  = (int) substr( $post_date, 0, 4 );

	$valid_date = wp_checkdate( $month, $day, $year, $post_date );

	if ( ! $valid_date ) {
		return false;
	}
	return $post_date;
}

常見問題

FAQs
檢視更多 >