1<?php
2
3/**
4 * FeedDate is an internal class that stores a date for a feed or feed item.
5 * Usually, you won't need to use this.
6 */
7class FeedDate
8{
9    protected $unix;
10
11    /**
12     * Creates a new instance of FeedDate representing a given date.
13     * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
14     *
15     * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and
16     *                          time is used.
17     */
18    public function __construct($dateString = "")
19    {
20        if ($dateString == "") {
21            $dateString = date("r");
22        }
23
24        if (is_integer($dateString)) {
25            $this->unix = $dateString;
26
27            return;
28        }
29        $tzOffset = 0;
30        if (preg_match(
31            "~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~",
32            $dateString,
33            $matches
34        )) {
35            $months = Array(
36                "Jan" => 1,
37                "Feb" => 2,
38                "Mar" => 3,
39                "Apr" => 4,
40                "May" => 5,
41                "Jun" => 6,
42                "Jul" => 7,
43                "Aug" => 8,
44                "Sep" => 9,
45                "Oct" => 10,
46                "Nov" => 11,
47                "Dec" => 12,
48            );
49            $this->unix = mktime($matches[4], $matches[5], $matches[6], $months[$matches[2]], $matches[1], $matches[3]);
50            if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
51                $tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
52            } else {
53                if (strlen($matches[7]) == 1) {
54                    $oneHour = 3600;
55                    $ord = ord($matches[7]);
56                    if ($ord < ord("M")) {
57                        $tzOffset = (ord("A") - $ord - 1) * $oneHour;
58                    } elseif ($ord >= ord("M") AND $matches[7] != "Z") {
59                        $tzOffset = ($ord - ord("M")) * $oneHour;
60                    } elseif ($matches[7] == "Z") {
61                        $tzOffset = 0;
62                    }
63                }
64                switch ($matches[7]) {
65                    case "UT":
66                    case "GMT":
67                        $tzOffset = 0;
68                }
69            }
70            $this->unix += $tzOffset;
71
72            return;
73        }
74        if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~", $dateString, $matches)) {
75            $this->unix = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
76            if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
77                $tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
78            } else {
79                if ($matches[7] == "Z") {
80                    $tzOffset = 0;
81                }
82            }
83            $this->unix += $tzOffset;
84
85            return;
86        }
87        $this->unix = 0;
88    }
89
90    /**
91     * Gets the date stored in this FeedDate as an RFC 822 date.
92     *
93     * @return string a date in RFC 822 format
94     */
95    public function rfc822()
96    {
97        //return gmdate("r",$this->unix);
98        $date = gmdate("D, d M Y H:i:s O", $this->unix);
99
100        return $date;
101    }
102
103    /**
104     * Gets the date stored in this FeedDate as an ISO 8601 date.
105     *
106     * @return string a date in ISO 8601 format
107     */
108    public function iso8601()
109    {
110        $date = gmdate("Y-m-d\TH:i:sP", $this->unix);
111
112        return $date;
113    }
114
115    /**
116     * Gets the date stored in this FeedDate as unix time stamp.
117     *
118     * @return int a date as a unix time stamp
119     */
120    public function unix()
121    {
122        return $this->unix;
123    }
124}
125