1 <?php
2
3 /*
4 * This file is part of the ICanBoogie package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace ICanBoogie\HTTP;
13
14 /**
15 * A date time object that renders into a string formatted for HTTP header fields.
16 *
17 * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
18 */
19 class DateHeader extends \ICanBoogie\DateTime
20 {
21 static public function from($source, $timezone=null)
22 {
23 if ($source === null)
24 {
25 return static::none();
26 }
27
28 return parent::from($source, $timezone);
29 }
30
31 /**
32 * Returns a new {@link DateHeader} object.
33 *
34 * @param string|int|\DateTime $time If time is provided as a numeric value it is used as
35 * "@{$time}" and the time zone is set to UTC.
36 * @param \DateTimeZone|string $timezone A {@link \DateTimeZone} object representing the desired
37 * time zone. If the time zone is empty `utc` is used instead.
38 */
39 public function __construct($time='now', $timezone=null)
40 {
41 if ($time instanceof \DateTime)
42 {
43 $time = $time->getTimestamp();
44 }
45
46 if (is_numeric($time))
47 {
48 $time = '@' . $time;
49 $timezone = null;
50 }
51
52 parent::__construct($time, $timezone ?: 'utc');
53 }
54
55 /**
56 * Formats the instance according to the RFC 1123.
57 */
58 public function __toString()
59 {
60 return $this->is_empty ? '' : $this->utc->as_rfc1123;
61 }
62 }