Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

#85 - added timezone parameter to WP_JSON_DateTime::createFromFormat() #87

Merged
merged 3 commits into from
Mar 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions lib/class-wp-json-datetime.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
<?php
class WP_JSON_DateTime extends DateTime
{
/**
* Workaround for DateTime::createFromFormat on PHP > 5.2
* Found on http://stackoverflow.com/a/17084893/717643
*
* @param string $format The format that the passed in string should be in.
* @param string $string String representing the time.
* @param DateTimeZone $timezone A DateTimeZone object representing the desired time zone.
* @return Datetime
*/
public static function createFromFormat($format, $time, $timezone = null)
{
if ( method_exists('DateTime', 'createFromFormat') ) {
return parent::createFromFormat($format, $time, $timezone);
}
/**
* DateTime compatibility class
*
* @package WordPress
* @subpackage JSON API
* @version 0.9
*/
class WP_JSON_DateTime extends DateTime {
/**
* Workaround for DateTime::createFromFormat on PHP > 5.2
*
* @link http://stackoverflow.com/a/17084893/717643
*
* @param string $format The format that the passed in string should be in.
* @param string $string String representing the time.
* @param DateTimeZone $timezone A DateTimeZone object representing the desired time zone.
* @return Datetime
*/
public static function createFromFormat($format, $time, $timezone = null ) {
if ( is_null( $timezone ) ) {
$timezone = new DateTimeZone( date_default_timezone_get() );
}
if ( method_exists( 'DateTime', 'createFromFormat' ) ) {
return parent::createFromFormat( $format, $time, $timezone );
}

return new DateTime(date($format, strtotime($time)), $timezone);
}
return new DateTime( date( $format, strtotime( $time ) ), $timezone );
}
}
2 changes: 1 addition & 1 deletion lib/class-wp-json-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ public function parse_date( $date, $force_utc = false ) {
if ( strpos( $date, '.' ) !== false ) {
$date = preg_replace( '/\.\d+/', '', $date );
}
$datetime = WP_JSON_DateTime::createFromFormat( DateTime::RFC3339, $date );
$datetime = WP_JSON_DateTime::createFromFormat( DateTime::RFC3339, $date, $timezone );

return $datetime;
}
Expand Down