Skip to content

Commit

Permalink
DateTime: implements JsonSerializable, formats date in ISO 8601 forma…
Browse files Browse the repository at this point in the history
…t accepted by JavaScript's Date object (BC break)
  • Loading branch information
dg committed May 21, 2015
1 parent 209844d commit 5177b69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Utils/DateTime.php
Expand Up @@ -18,7 +18,7 @@
* *
* @author David Grudl * @author David Grudl
*/ */
class DateTime extends \DateTime class DateTime extends \DateTime implements \JsonSerializable
{ {
/** minute in seconds */ /** minute in seconds */
const MINUTE = 60; const MINUTE = 60;
Expand Down Expand Up @@ -126,6 +126,16 @@ public static function createFromFormat($format, $time, $timezone = NULL)
return $date ? static::from($date) : FALSE; return $date ? static::from($date) : FALSE;
} }



/**
* Returns JSON representation in ISO 8601 (used by JavaScript).
* @return string
*/
public function jsonSerialize()
{
return $this->format('c');
}

} }


}); });
15 changes: 15 additions & 0 deletions tests/Utils/DateTime.JSON.phpt
@@ -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.