get_post_time( string $format = 'U', bool $gmt = false, int|WP_Post|null $post = null, bool $translate = false ): string|int|false
- Since
- 2.0.0
- Source
wp-includes/general-template.php:3094
Compatibility
- WordPress
- since 2.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$formatstringoptional- Format to use for retrieving the time the post was written. Accepts 'G', 'U', or PHP date format. Default 'U'.Default:
'U' $gmtbooloptional- Whether to retrieve the GMT time. Default false.Default:
false $postint|WP_Post|nulloptional- Post ID or post object. Default is global
$postobject.Default:null $translatebooloptional- Whether to translate the time string. Default false.Default:
false
Return value
string|int|false- Formatted date string or Unix timestamp if
$formatis 'U' or 'G'.
False on failure.
Performance profile
How much work a call to get_post_time() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 10–48
- Plugin surface
- 1 hook
- Called by
- 5
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 72.
Third-party callbacks on 'get_post_time' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 7 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_post_time() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | get_post() |
$datetime === false | 21–22 | get_post(), get_post_datetime() |
$datetime !== false | 33–44 | get_post(), get_post_datetime(), ->getTimestamp(), apply_filters() |
$datetime !== false && $format !== "U" && $format !== "G" | 37–38 | get_post(), get_post_datetime(), ->format(), apply_filters() |
$datetime !== false | 37–40 | get_post(), get_post_datetime(), ->getTimestamp(), ->getOffset(), apply_filters() |
$datetime !== false && $format !== "U" && $format !== "G" | 44–45 | get_post(), get_post_datetime(), ->setTimezone(), ->format(), apply_filters() |
$datetime !== false && $format !== "U" && $format !== "G" | 47–48 | get_post(), get_post_datetime(), ->getTimestamp(), wp_date(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 72 instructions, 10–48 executed per call, 9 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_post_time() runs, in this order:
- apply_filters( get_post_time )filterline 3135 (+41 into the body)
Filters the localized time of the post.
Uses · 5
- get_post()Retrieves post data given a post ID or post object.
- get_post_datetime()Retrieves post published or modified time as a `DateTimeImmutable` object instance.
- wp_date()Retrieves the date, in localized format.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- DateTimeZone::__construct()
Used by · 5
- export_wp()Generates the WXR export file for download.
- get_the_date()Retrieves the date of the post.
- get_the_time()Retrieves the time of the post.
- the_weekday()Displays the localized weekday for the post.
- the_weekday_date()Displays the localized weekday for the post.
Source code
function get_post_time( $format = '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' === $format || 'G' === $format ) { $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( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null ); } else { if ( $gmt ) { $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); } $time = $datetime->format( $format ); } /** * Filters the localized time of the post. * * @since 2.6.0 * * @param string|int $time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'. * @param string $format Format to use for retrieving the date of the post. * Accepts 'G', 'U', or PHP date format. * @param bool $gmt Whether to retrieve the GMT time. */ return apply_filters( 'get_post_time', $time, $format, $gmt );}Changelog
Introduced in 2.0.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$post retyped from int|WP_Post to int|WP_Post|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/general-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.