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