get_post_time( string $d = 'U', bool $gmt = false, int|WP_Post $post = null, bool $translate = false )

Retrieve the time at which the post was written.


Description Description


Parameters Parameters

$d

(string) (Optional) Format to use for retrieving the time the post was written. Either 'G', 'U', or php date format.

Default value: 'U'

$gmt

(bool) (Optional) Whether to retrieve the GMT time.

Default value: false

$post

(int|WP_Post) (Optional) WP_Post object or ID. Default is global $post object.

Default value: null

$translate

(bool) (Optional) Whether to translate the time string.

Default value: false


Top ↑

Return Return

(string|int|false) Formatted date string or Unix timestamp if $d is 'U' or 'G'. False on failure.


Top ↑

Source Source

File: wp-includes/general-template.php

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

	if ( ! $post ) {
		return false;
	}

	$source   = ( $gmt ) ? 'gmt' : 'local';
	$datetime = get_post_datetime( $post, 'date', $source );

	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 );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.