rest_get_date_with_gmt

Parses a date into both its local and UTC equivalent, in MySQL datetime format.

Description

(array|null) rest_get_date_with_gmt( (string) $date, (bool) $is_utc = false ); 

Returns (array|null)

Local and UTC datetime strings, in MySQL datetime format (Y-m-d H:i:s), null on failure.

Parameters (2)

0. $date (string)
RFC3339 timestamp.
1. $is_utc — Optional. (bool) => false
Whether the provided date should be interpreted as UTC. Default false.

Usage

  1. if ( !function_exists( 'rest_get_date_with_gmt' ) ) { 
  2. require_once ABSPATH . WPINC . '/rest-api.php'; 
  3.  
  4. // RFC3339 timestamp. 
  5. $date = ''; 
  6.  
  7. // Whether the provided date should be interpreted as UTC. Default false. 
  8. $is_utc = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = rest_get_date_with_gmt($date, $is_utc); 
  12.  

Defined (1)

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

/wp-includes/rest-api.php  
  1. function rest_get_date_with_gmt( $date, $is_utc = false ) { 
  2. // Whether or not the original date actually has a timezone string 
  3. // changes the way we need to do timezone conversion. Store this info 
  4. // before parsing the date, and use it later. 
  5. $has_timezone = preg_match( '#(Z|[+-]\d{2}(:\d{2})?)$#', $date ); 
  6.  
  7. $date = rest_parse_date( $date ); 
  8.  
  9. if ( empty( $date ) ) { 
  10. return null; 
  11.  
  12. // At this point $date could either be a local date (if we were passed a 
  13. // *local* date without a timezone offset) or a UTC date (otherwise). 
  14. // Timezone conversion needs to be handled differently between these two 
  15. // cases. 
  16. if ( ! $is_utc && ! $has_timezone ) { 
  17. $local = date( 'Y-m-d H:i:s', $date ); 
  18. $utc = get_gmt_from_date( $local ); 
  19. } else { 
  20. $utc = date( 'Y-m-d H:i:s', $date ); 
  21. $local = get_date_from_gmt( $utc ); 
  22.  
  23. return array( $local, $utc );