WPCOM_JSON_API_Date

The WordPress Core WPCOM JSON API Date class.

Defined (1)

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

/sal/class.json-api-date.php  
  1. class WPCOM_JSON_API_Date { 
  2. /** 
  3. * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00 
  4. * @param $date_gmt (string) GMT datetime string. 
  5. * @param $date (string) Optional. Used to calculate the offset from GMT. 
  6. * @return string 
  7. */ 
  8. static function format_date( $date_gmt, $date = null ) { 
  9. $timestamp_gmt = strtotime( "$date_gmt+0000" ); 
  10.  
  11. if ( null === $date ) { 
  12. $timestamp = $timestamp_gmt; 
  13. $hours = $minutes = $west = 0; 
  14. } else { 
  15. $date_time = date_create( "$date+0000" ); 
  16. if ( $date_time ) { 
  17. $timestamp = date_format( $date_time, 'U' ); 
  18. } else { 
  19. $timestamp = 0; 
  20.  
  21. // "0000-00-00 00:00:00" == -62169984000 
  22. if ( - 62169984000 == $timestamp_gmt ) { 
  23. // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts 
  24. // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts 
  25.  
  26. // Try to guess the correct offset from the blog's options. 
  27. $timezone_string = get_option( 'timezone_string' ); 
  28.  
  29. if ( $timezone_string && $date_time ) { 
  30. $timezone = timezone_open( $timezone_string ); 
  31. if ( $timezone ) { 
  32. $offset = $timezone->getOffset( $date_time ); 
  33. } else { 
  34. $offset = 3600 * get_option( 'gmt_offset' ); 
  35. } else { 
  36. $offset = $timestamp - $timestamp_gmt; 
  37.  
  38. $west = $offset < 0; 
  39. $offset = abs( $offset ); 
  40. $hours = (int) floor( $offset / 3600 ); 
  41. $offset -= $hours * 3600; 
  42. $minutes = (int) floor( $offset / 60 ); 
  43.  
  44. return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes );