get_post_time

Retrieve the time at which the post was written.

Description

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

Returns (string|int|false)

Formatted date string or Unix timestamp if `$id` is 'U' or 'G'. False on failure.

Parameters (4)

0. $d — Optional. (string) => 'U'
Format to use for retrieving the time the post was written. Either G,, U., or php date format. Default U..
1. $gmt — Optional. (constant) => false
Whether to retrieve the GMT time. Default false.
2. $post — Optional. (constant) => null
WP_Post object or ID. Default is global $post object.
3. $translate — Optional. (bool) => false
Whether to translate the time string. Default false.

Usage

  1. if ( !function_exists( 'get_post_time' ) ) { 
  2. require_once ABSPATH . WPINC . '/general-template.php'; 
  3.  
  4. // Optional. Format to use for retrieving the time the post 
  5. // was written. Either 'G', 'U', or php date format. Default 'U'. 
  6. $d = 'U'; 
  7.  
  8. // Optional. Whether to retrieve the GMT time. Default false. 
  9. $gmt = false; 
  10.  
  11. // WP_Post object or ID. Default is global $post object. 
  12. $post = null; 
  13.  
  14. // Whether to translate the time string. Default false. 
  15. $translate = false; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = get_post_time($d, $gmt, $post, $translate); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/general-template.php  
  1. function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false ) { 
  2. $post = get_post($post); 
  3.  
  4. if ( ! $post ) { 
  5. return false; 
  6.  
  7. if ( $gmt ) 
  8. $time = $post->post_date_gmt; 
  9. else 
  10. $time = $post->post_date; 
  11.  
  12. $time = mysql2date($d, $time, $translate); 
  13.  
  14. /** 
  15. * Filters the localized time a post was written. 
  16. * @since 2.6.0 
  17. * @param string $time The formatted time. 
  18. * @param string $d Format to use for retrieving the time the post was written. 
  19. * Accepts 'G', 'U', or php date format. Default 'U'. 
  20. * @param bool $gmt Whether to retrieve the GMT time. Default false. 
  21. */ 
  22. return apply_filters( 'get_post_time', $time, $d, $gmt );