xref: /dokuwiki/vendor/openpsa/universalfeedcreator/lib/Element/FeedDate.php (revision e43cd7e11322648414daef21f777734a2cafc5c8)
1572dd708SAndreas Gohr<?php
2572dd708SAndreas Gohr
3572dd708SAndreas Gohr/**
4572dd708SAndreas Gohr * FeedDate is an internal class that stores a date for a feed or feed item.
5572dd708SAndreas Gohr * Usually, you won't need to use this.
6572dd708SAndreas Gohr */
7572dd708SAndreas Gohrclass FeedDate
8572dd708SAndreas Gohr{
9572dd708SAndreas Gohr    protected $unix;
10572dd708SAndreas Gohr
11572dd708SAndreas Gohr    /**
12572dd708SAndreas Gohr     * Creates a new instance of FeedDate representing a given date.
13572dd708SAndreas Gohr     * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
14572dd708SAndreas Gohr     *
15572dd708SAndreas Gohr     * @param mixed $dateString optional the date this FeedDate will represent. If not specified, the current date and
16572dd708SAndreas Gohr     *                          time is used.
17572dd708SAndreas Gohr     */
18572dd708SAndreas Gohr    public function __construct($dateString = "")
19572dd708SAndreas Gohr    {
20572dd708SAndreas Gohr        if ($dateString == "") {
21572dd708SAndreas Gohr            $dateString = date("r");
22572dd708SAndreas Gohr        }
23572dd708SAndreas Gohr
24572dd708SAndreas Gohr        if (is_integer($dateString)) {
25572dd708SAndreas Gohr            $this->unix = $dateString;
26572dd708SAndreas Gohr
27572dd708SAndreas Gohr            return;
28572dd708SAndreas Gohr        }
29572dd708SAndreas Gohr        $tzOffset = 0;
30572dd708SAndreas Gohr        if (preg_match(
31572dd708SAndreas Gohr            "~(?:(?: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+(.*)~",
32572dd708SAndreas Gohr            $dateString,
33572dd708SAndreas Gohr            $matches
34572dd708SAndreas Gohr        )) {
35572dd708SAndreas Gohr            $months = Array(
36572dd708SAndreas Gohr                "Jan" => 1,
37572dd708SAndreas Gohr                "Feb" => 2,
38572dd708SAndreas Gohr                "Mar" => 3,
39572dd708SAndreas Gohr                "Apr" => 4,
40572dd708SAndreas Gohr                "May" => 5,
41572dd708SAndreas Gohr                "Jun" => 6,
42572dd708SAndreas Gohr                "Jul" => 7,
43572dd708SAndreas Gohr                "Aug" => 8,
44572dd708SAndreas Gohr                "Sep" => 9,
45572dd708SAndreas Gohr                "Oct" => 10,
46572dd708SAndreas Gohr                "Nov" => 11,
47572dd708SAndreas Gohr                "Dec" => 12,
48572dd708SAndreas Gohr            );
49572dd708SAndreas Gohr            $this->unix = mktime($matches[4], $matches[5], $matches[6], $months[$matches[2]], $matches[1], $matches[3]);
50572dd708SAndreas Gohr            if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
51572dd708SAndreas Gohr                $tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
52572dd708SAndreas Gohr            } else {
53572dd708SAndreas Gohr                if (strlen($matches[7]) == 1) {
54572dd708SAndreas Gohr                    $oneHour = 3600;
55572dd708SAndreas Gohr                    $ord = ord($matches[7]);
56572dd708SAndreas Gohr                    if ($ord < ord("M")) {
57572dd708SAndreas Gohr                        $tzOffset = (ord("A") - $ord - 1) * $oneHour;
58572dd708SAndreas Gohr                    } elseif ($ord >= ord("M") AND $matches[7] != "Z") {
59572dd708SAndreas Gohr                        $tzOffset = ($ord - ord("M")) * $oneHour;
60572dd708SAndreas Gohr                    } elseif ($matches[7] == "Z") {
61572dd708SAndreas Gohr                        $tzOffset = 0;
62572dd708SAndreas Gohr                    }
63572dd708SAndreas Gohr                }
64572dd708SAndreas Gohr                switch ($matches[7]) {
65572dd708SAndreas Gohr                    case "UT":
66572dd708SAndreas Gohr                    case "GMT":
67572dd708SAndreas Gohr                        $tzOffset = 0;
68572dd708SAndreas Gohr                }
69572dd708SAndreas Gohr            }
70572dd708SAndreas Gohr            $this->unix += $tzOffset;
71572dd708SAndreas Gohr
72572dd708SAndreas Gohr            return;
73572dd708SAndreas Gohr        }
74572dd708SAndreas Gohr        if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~", $dateString, $matches)) {
75572dd708SAndreas Gohr            $this->unix = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
76572dd708SAndreas Gohr            if (substr($matches[7], 0, 1) == '+' OR substr($matches[7], 0, 1) == '-') {
77572dd708SAndreas Gohr                $tzOffset = (((int)substr($matches[7], 0, 3) * 60) + (int)substr($matches[7], -2)) * 60;
78572dd708SAndreas Gohr            } else {
79572dd708SAndreas Gohr                if ($matches[7] == "Z") {
80572dd708SAndreas Gohr                    $tzOffset = 0;
81572dd708SAndreas Gohr                }
82572dd708SAndreas Gohr            }
83572dd708SAndreas Gohr            $this->unix += $tzOffset;
84572dd708SAndreas Gohr
85572dd708SAndreas Gohr            return;
86572dd708SAndreas Gohr        }
87572dd708SAndreas Gohr        $this->unix = 0;
88572dd708SAndreas Gohr    }
89572dd708SAndreas Gohr
90572dd708SAndreas Gohr    /**
91572dd708SAndreas Gohr     * Gets the date stored in this FeedDate as an RFC 822 date.
92572dd708SAndreas Gohr     *
93572dd708SAndreas Gohr     * @return string a date in RFC 822 format
94572dd708SAndreas Gohr     */
95572dd708SAndreas Gohr    public function rfc822()
96572dd708SAndreas Gohr    {
97572dd708SAndreas Gohr        //return gmdate("r",$this->unix);
98572dd708SAndreas Gohr        $date = gmdate("D, d M Y H:i:s O", $this->unix);
99572dd708SAndreas Gohr
100572dd708SAndreas Gohr        return $date;
101572dd708SAndreas Gohr    }
102572dd708SAndreas Gohr
103572dd708SAndreas Gohr    /**
104572dd708SAndreas Gohr     * Gets the date stored in this FeedDate as an ISO 8601 date.
105572dd708SAndreas Gohr     *
106572dd708SAndreas Gohr     * @return string a date in ISO 8601 format
107572dd708SAndreas Gohr     */
108572dd708SAndreas Gohr    public function iso8601()
109572dd708SAndreas Gohr    {
110*e43cd7e1SAndreas Gohr        $date = gmdate("Y-m-d\TH:i:sP", $this->unix);
111572dd708SAndreas Gohr
112572dd708SAndreas Gohr        return $date;
113572dd708SAndreas Gohr    }
114572dd708SAndreas Gohr
115572dd708SAndreas Gohr    /**
116572dd708SAndreas Gohr     * Gets the date stored in this FeedDate as unix time stamp.
117572dd708SAndreas Gohr     *
118572dd708SAndreas Gohr     * @return int a date as a unix time stamp
119572dd708SAndreas Gohr     */
120572dd708SAndreas Gohr    public function unix()
121572dd708SAndreas Gohr    {
122572dd708SAndreas Gohr        return $this->unix;
123572dd708SAndreas Gohr    }
124572dd708SAndreas Gohr}
125