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