diff options
Diffstat (limited to 'plugins/jetpack/sal/class.json-api-date.php')
-rw-r--r-- | plugins/jetpack/sal/class.json-api-date.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/plugins/jetpack/sal/class.json-api-date.php b/plugins/jetpack/sal/class.json-api-date.php new file mode 100644 index 00000000..d51247c0 --- /dev/null +++ b/plugins/jetpack/sal/class.json-api-date.php @@ -0,0 +1,55 @@ +<?php + +class WPCOM_JSON_API_Date { + /** + * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00 + * + * @param $date_gmt (string) GMT datetime string. + * @param $date (string) Optional. Used to calculate the offset from GMT. + * + * @return string + */ + static function format_date( $date_gmt, $date = null ) { + $timestamp_gmt = strtotime( "$date_gmt+0000" ); + + if ( null === $date ) { + $timestamp = $timestamp_gmt; + $hours = $minutes = $west = 0; + } else { + $date_time = date_create( "$date+0000" ); + if ( $date_time ) { + $timestamp = date_format( $date_time, 'U' ); + } else { + $timestamp = 0; + } + + // "0000-00-00 00:00:00" == -62169984000 + if ( - 62169984000 == $timestamp_gmt ) { + // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts + // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts + + // Try to guess the correct offset from the blog's options. + $timezone_string = get_option( 'timezone_string' ); + + if ( $timezone_string && $date_time ) { + $timezone = timezone_open( $timezone_string ); + if ( $timezone ) { + $offset = $timezone->getOffset( $date_time ); + } + } else { + $offset = 3600 * get_option( 'gmt_offset' ); + } + } else { + $offset = $timestamp - $timestamp_gmt; + } + + $west = $offset < 0; + $offset = abs( $offset ); + $hours = (int) floor( $offset / 3600 ); + $offset -= $hours * 3600; + $minutes = (int) floor( $offset / 60 ); + } + + return (string) gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); + } +}
\ No newline at end of file |