Outil de recherche WordPress pour les développeurs et auteurs de thèmes



get_post_time ›

Depuis2.0.0
Dépréciéen/a
get_post_time ( $d = 'U', $gmt = false, $post = null, $translate = false )
Paramètres: (4)
  • (string) $d Optional. Format to use for retrieving the time the post was written. Either 'G', 'U', or php date format. Default 'U'.
    Requis: Non
    Défaut: 'U'
  • (bool) $gmt Optional. Whether to retrieve the GMT time. Default false.
    Requis: Non
    Défaut: false
  • (int|WP_Post) $post WP_Post object or ID. Default is global `$post` object.
    Requis: Non
    Défaut: null
  • (bool) $translate Whether to translate the time string. Default false.
    Requis: Non
    Défaut: false
Retourne:
  • (string|int|false) Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
Défini(e) dans:
Codex:

Retrieve the time at which the post was written.



Source

function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$datetime = get_post_datetime( $post );

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

	if ( 'U' === $d || 'G' === $d ) {
		$time = $datetime->getTimestamp();

		// Returns a sum of timestamp with timezone offset. Ideally should never be used.
		if ( ! $gmt ) {
			$time += $datetime->getOffset();
		}
	} elseif ( $translate ) {
		$time = wp_date( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
	} else {
		if ( $gmt ) {
			$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
		}

		$time = $datetime->format( $d );
	}

	/**
	 * Filters the localized time a post was written.
	 *
	 * @since 2.6.0
	 *
	 * @param string $time The formatted time.
	 * @param string $d    Format to use for retrieving the time the post was written.
	 *                     Accepts 'G', 'U', or php date format. Default 'U'.
	 * @param bool   $gmt  Whether to retrieve the GMT time. Default false.
	 */
	return apply_filters( 'get_post_time', $time, $d, $gmt );
}