Permalink
Browse files

DateTime: implements JsonSerializable, formats date in ISO 8601 forma…

…t accepted by JavaScript's Date object (BC break)
  • Loading branch information...
dg committed May 21, 2015
1 parent 209844d commit 5177b69e8dc33b8b7ce9329cb1c36387133ced8f
Showing with 26 additions and 1 deletion.
  1. +11 −1 src/Utils/DateTime.php
  2. +15 −0 tests/Utils/DateTime.JSON.phpt
@@ -18,7 +18,7 @@
*
* @author David Grudl
*/
class DateTime extends \DateTime
class DateTime extends \DateTime implements \JsonSerializable
{
/** minute in seconds */
const MINUTE = 60;
@@ -126,6 +126,16 @@ public static function createFromFormat($format, $time, $timezone = NULL)
return $date ? static::from($date) : FALSE;
}
/**
* Returns JSON representation in ISO 8601 (used by JavaScript).
* @return string
*/
public function jsonSerialize()
{
return $this->format('c');
}
}
});
@@ -0,0 +1,15 @@
<?php
/**
* Test: Nette\Utils\DateTime & JSON.
*/
use Tester\Assert,
Nette\Utils\DateTime;
require __DIR__ . '/../bootstrap.php';
date_default_timezone_set('Europe/Prague');
Assert::same( '"1978-01-23T11:40:00+01:00"', json_encode(DateTime::from(254400000)) );

0 comments on commit 5177b69

Please sign in to comment.