get_the_time()
Gets the time at which the current post was written in the specified format. Used inside the Loop of WordPress.
Uses: get_post_time()
Used By: the_time()
Hooks from the function
Return
String|Int|false
. Formatted date string or Unix timestamp if $format is 'U' or 'G'. False on failure.
Usage
get_the_time( $format, $post );
- $format(string)
- Format to use for retrieving the time the post was written. Accepts 'G', 'U', or PHP date format. option.
Default: 'time_format' - $post(int|WP_Post)
- Post ID or post object.
Default: global $post object
Examples
#1 Basic usage
Output the time of publication of the current post in the loop. The date format will be as set in the WP settings:
<?php $local_timestamp = get_the_time('U'); ?>
Do the same, just indicate the desired post:
<?php echo get_the_time('', $post->ID ); ?>
#2 Getting the time stamp in Unix format
To get the Unix timestamp of the post written date (the number of seconds passed from January 1, 1970 to the written date), the first parameter must be set to "U"
.
<?php $local_timestamp = get_the_time('U'); ?>
#3 The time in the GMT time zone
Sometimes you want to get the time in the GMT (UTC) time zone, not the current date of publication (written). To do this, specify the second parameter:
<?php $gmt_timestamp = get_post_time('U', true); ?>
Changelog
Since 1.5.0 | Introduced. |