'D, d M Y H:i:s \G\M\T', self::DATE_RFC1036 => 'D, d M y H:i:s \G\M\T', self::DATE_ANSIC => 'D M j H:i:s Y', ); /** * Create date-based header from string * * @param string $headerLine * @return AbstractDate * @throws Exception\InvalidArgumentException */ public static function fromString($headerLine) { $dateHeader = new static(); list($name, $date) = GenericHeader::splitHeaderLine($headerLine); // check to ensure proper header type for this factory if (strtolower($name) !== strtolower($dateHeader->getFieldName())) { throw new Exception\InvalidArgumentException( 'Invalid header line for "' . $dateHeader->getFieldName() . '" header string' ); } $dateHeader->setDate($date); return $dateHeader; } /** * Set date output format * * @param int $format * @throws Exception\InvalidArgumentException */ public static function setDateFormat($format) { if (!isset(static::$dateFormats[$format])) { throw new Exception\InvalidArgumentException( "No constant defined for provided date format: {$format}" ); } static::$dateFormat = static::$dateFormats[$format]; } /** * Return current date output format * * @return string */ public static function getDateFormat() { return static::$dateFormat; } /** * Set the date for this header, this can be a string or an instance of \DateTime * * @param string|DateTime $date * @return AbstractDate * @throws Exception\InvalidArgumentException */ public function setDate($date) { if (is_string($date)) { try { $date = new DateTime($date, new DateTimeZone('GMT')); } catch (\Exception $e) { throw new Exception\InvalidArgumentException( sprintf('Invalid date passed as string (%s)', (string) $date), $e->getCode(), $e ); } } elseif (!($date instanceof DateTime)) { throw new Exception\InvalidArgumentException('Date must be an instance of \DateTime or a string'); } $date->setTimezone(new DateTimeZone('GMT')); $this->date = $date; return $this; } /** * Return date for this header * * @return string */ public function getDate() { return $this->date()->format(static::$dateFormat); } /** * Return date for this header as an instance of \DateTime * * @return DateTime */ public function date() { if ($this->date === null) { $this->date = new DateTime(null, new DateTimeZone('GMT')); } return $this->date; } /** * Compare provided date to date for this header * Returns < 0 if date in header is less than $date; > 0 if it's greater, and 0 if they are equal. * @see \strcmp() * * @param string|DateTime $date * @return int * @throws Exception\InvalidArgumentException */ public function compareTo($date) { if (is_string($date)) { try { $date = new DateTime($date, new DateTimeZone('GMT')); } catch (\Exception $e) { throw new Exception\InvalidArgumentException( sprintf('Invalid Date passed as string (%s)', (string) $date), $e->getCode(), $e ); } } elseif (!($date instanceof DateTime)) { throw new Exception\InvalidArgumentException('Date must be an instance of \DateTime or a string'); } $dateTimestamp = $date->getTimestamp(); $thisTimestamp = $this->date()->getTimestamp(); return ($thisTimestamp === $dateTimestamp) ? 0 : (($thisTimestamp > $dateTimestamp) ? 1 : -1); } /** * Get header value as formatted date * * @return string */ public function getFieldValue() { return $this->getDate(); } /** * Return header line * * @return string */ public function toString() { return $this->getFieldName() . ': ' . $this->getDate(); } /** * Allow casting to string * * @return string */ public function __toString() { return $this->toString(); } }