get_post_datetime

Advertisement

Summery Summery

Retrieve post published or modified time as a DateTimeImmutable object instance.

Syntax Syntax

get_post_datetime( int|WP_Post $post = null, string $field = 'date', string $source = 'local' )

Description Description

The object will be set to the timezone from WordPress settings.

For legacy reasons, this function allows to choose to instantiate from local or UTC time in database. Normally this should make no difference to the result. However, the values might get out of sync in database, typically because of timezone setting changes. The parameter ensures the ability to reproduce backwards compatible behaviors in such cases.

Parameters Parameters

$post

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

Default value: null

$field

(string) (Optional) Published or modified time to use from database. Accepts 'date' or 'modified'.

Default value: 'date'

$source

(string) (Optional) Local or UTC time to use from database. Accepts 'local' or 'gmt'.

Default value: 'local'

Return Return

(DateTimeImmutable|false) Time object on success, false on failure.

Source Source

File: wp-includes/general-template.php

	 * @since 0.71
	 *
	 * @param string $get_the_time The formatted time.
	 * @param string $format       The time format. Accepts 'G', 'U',
	 *                             or PHP date format.
	 */
	echo apply_filters( 'the_time', get_the_time( $format ), $format );
}

/**
 * Retrieve the time at which the post was written.
 *
 * @since 1.5.0
 *
 * @param string      $format Optional. Format to use for retrieving the time the post
 *                            was written. Either 'G', 'U', or PHP date format defaults
 *                            to the value specified in the time_format option. Default empty.
 * @param int|WP_Post $post   WP_Post object or ID. Default is global `$post` object.
 * @return string|int|false Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
 *                          False on failure.
 */
function get_the_time( $format = '', $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$_format = ! empty( $format ) ? $format : get_option( 'time_format' );

Advertisement

Changelog Changelog

Changelog
Version Description
5.3.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.