iso8601_to_datetime

Converts an iso8601 date to MySQL DateTime format used by post_date[_gmt].

Description

(string) iso8601_to_datetime( (string) $date_string, (string) $timezone = 'user' ); 

Returns (string)

The date and time in MySQL DateTime format - Y-m-d H:i:s.

Parameters (2)

0. $date_string (string)
The date string.
1. $timezone — Optional. (string) => 'user'
If set to GMT returns the time minus gmt_offset. Default is user..

Usage

  1. if ( !function_exists( 'iso8601_to_datetime' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The date string. 
  5. $date_string = ''; 
  6.  
  7. // Optional. If set to GMT returns the time minus gmt_offset. Default is 'user'. 
  8. $timezone = 'user'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = iso8601_to_datetime($date_string, $timezone); 
  12.  

Defined (1)

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

/wp-includes/formatting.php  
  1. function iso8601_to_datetime( $date_string, $timezone = 'user' ) { 
  2. $timezone = strtolower($timezone); 
  3.  
  4. if ($timezone == 'gmt') { 
  5.  
  6. preg_match('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2, 4}) {0, 1}#', $date_string, $date_bits); 
  7.  
  8. if (!empty($date_bits[7])) { // we have a timezone, so let's compute an offset 
  9. $offset = iso8601_timezone_to_offset($date_bits[7]); 
  10. } else { // we don't have a timezone, so we assume user local timezone (not server's!) 
  11. $offset = HOUR_IN_SECONDS * get_option('gmt_offset'); 
  12.  
  13. $timestamp = gmmktime($date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1]); 
  14. $timestamp -= $offset; 
  15.  
  16. return gmdate('Y-m-d H:i:s', $timestamp); 
  17.  
  18. } elseif ($timezone == 'user') { 
  19. return preg_replace('#([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(Z|[\+|\-][0-9]{2, 4}) {0, 1}#', '$1-$2-$3 $4:$5:$6', $date_string);