Title: get_post_datetime
Published: November 12, 2019
Last modified: February 24, 2026

---

# get_post_datetime( int|WP_Post $post = null, string $field, string $source ): DateTimeImmutable|false

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#wp--skip-link--target)

Retrieves post published or modified time as a `DateTimeImmutable` object instance.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#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](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#parameters)󠁿

 `$post`int|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
optional

Post ID or post object. Default is global `$post` object.

Default:`null`

`$field`stringoptional

Published or modified time to use from database. Accepts `'date'` or `'modified'`.

Default `'date'`.

`$source`stringoptional

Local or UTC time to use from database. Accepts `'local'` or `'gmt'`.
 Default `'
local'`.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#source)󠁿

    ```php
    function get_post_datetime( $post = null, $field = 'date', $source = 'local' ) {
    	$post = get_post( $post );

    	if ( ! $post ) {
    		return false;
    	}

    	$wp_timezone = wp_timezone();

    	if ( 'gmt' === $source ) {
    		$time     = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt;
    		$timezone = new DateTimeZone( 'UTC' );
    	} else {
    		$time     = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
    		$timezone = $wp_timezone;
    	}

    	if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
    		return false;
    	}

    	$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone );

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

    	return $datetime->setTimezone( $wp_timezone );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/general-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/general-template.php#L2941)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/general-template.php#L2941-L2969)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_timezone()](https://developer.wordpress.org/reference/functions/wp_timezone/)`wp-includes/functions.php` |

Retrieves the timezone of the site as a `DateTimeZone` object.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

| Used by | Description | 
| [get_post_timestamp()](https://developer.wordpress.org/reference/functions/get_post_timestamp/)`wp-includes/general-template.php` |

Retrieves post published or modified time as a Unix timestamp.

  | 
| [get_post_time()](https://developer.wordpress.org/reference/functions/get_post_time/)`wp-includes/general-template.php` |

Retrieves the localized time of the post.

  | 
| [get_post_modified_time()](https://developer.wordpress.org/reference/functions/get_post_modified_time/)`wp-includes/general-template.php` |

Retrieves the time at which the post was last modified.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_post_datetime/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.3.0](https://developer.wordpress.org/reference/since/5.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_post_datetime%2F)
before being able to contribute a note or feedback.