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 string $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); } 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; } elseif (is_int($value)) { $value = new DateTime('@' . $value); } elseif (!$value instanceof DateTime) { $value = new DateTime($value); } return $value->format($this->format); } }