Source of file DateTimeFormatter.php
Size: 2,359 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Filter/DateTimeFormatter.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Filter; use DateTime; class DateTimeFormatter extends AbstractFilter { /** * A valid format string accepted by date() * * @var string */ protected $format = DateTime::ISO8601; /** * Sets filter options * * @param array|Traversable $options */ public function __construct($options = null) { if ($options) { $this->setOptions($options); } } /** * Set the format string accepted by date() to use when formatting a string * * @param string $format * @return self */ public function setFormat($format) { $this->format = $format; return $this; } /** * Filter a datetime string by normalizing it to the filters specified format * * @param DateTime|string|integer $value * @throws Exception\InvalidArgumentException * @return string */ public function filter($value) { try { $result = $this->normalizeDateTime($value); } catch (\Exception $e) { // DateTime threw an exception, an invalid date string was provided throw new Exception\InvalidArgumentException('Invalid date string provided', $e->getCode(), $e); } if ($result === false) { return $value; } return $result; } /** * Normalize the provided value to a formatted string * * @param string|int|DateTime $value * @return string */ protected function normalizeDateTime($value) { if ($value === '' || $value === null) { return $value; } if (!is_string($value) && !is_int($value) && !$value instanceof DateTime) { return $value; } if (is_int($value)) { //timestamp $value = new DateTime('@' . $value); } elseif (!$value instanceof DateTime) { $value = new DateTime($value); } return $value->format($this->format); } } |