xref: /plugin/combo/ComboStrap/Iso8601Date.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
1<?php
2
3
4namespace ComboStrap;
5
6
7use DateTime;
8
9
10/**
11 * Class Is8601Date
12 * @package ComboStrap
13 * Format used by Google, Sqlite and others
14 */
15class Iso8601Date
16{
17    /**
18     * @var DateTime|false
19     */
20    private $dateTime;
21
22
23    /**
24     * Date constructor.
25     */
26    public function __construct($dateTime = null)
27    {
28
29        if ($dateTime == null) {
30
31            $this->dateTime = new DateTime();
32
33        } else {
34
35            $this->dateTime = $dateTime;
36
37        }
38
39    }
40
41    public static function create($string = null)
42    {
43        if ($string === null) {
44            return new Iso8601Date();
45        }
46
47
48        /**
49         * Time ?
50         * (ie only YYYY-MM-DD)
51         */
52        if (strlen($string) <= 10) {
53            /**
54             * We had the time to 00:00:00
55             * because {@link DateTime::createFromFormat} with a format of
56             * Y-m-d will be using the actual time otherwise
57             *
58             */
59            $string .= "T00:00:00";
60        }
61
62        /**
63         * Timezone
64         */
65        if (strlen($string) <= 19) {
66            /**
67             * Because this text metadata may be used in other part of the application
68             * We add the timezone to make it whole
69             * And to have a consistent value
70             */
71            $string .= date('P');
72        }
73
74        /**
75         * Date validation
76         * Atom is the valid ISO format (and not IS8601 due to backward compatibility)
77         *
78         * See:
79         * https://www.php.net/manual/en/class.datetimeinterface.php#datetime.constants.iso8601
80         */
81        $dateTime = DateTime::createFromFormat(DateTime::ATOM, $string);
82        return new Iso8601Date($dateTime);
83    }
84
85    public static function createFromTimestamp($timestamp)
86    {
87       $dateTime = new DateTime();
88       $dateTime->setTimestamp($timestamp);
89        return new Iso8601Date($dateTime);
90    }
91
92    /**
93     * And note {@link DATE_ISO8601}
94     * because it's not the compliant IS0-8601 format
95     * as explained here
96     * https://www.php.net/manual/en/class.datetimeinterface.php#datetime.constants.iso8601
97     * ATOM is
98     *
99     * This format is used by Sqlite, Google and is pretty the standard everywhere
100     * https://www.w3.org/TR/NOTE-datetime
101     */
102    public static function getFormat()
103    {
104        return DATE_ATOM;
105    }
106
107    public function isValidDateEntry()
108    {
109        if ($this->dateTime !== false) {
110            return true;
111        } else {
112            return false;
113        }
114    }
115
116    public function getDateTime()
117    {
118        return $this->dateTime;
119    }
120
121    public function __toString()
122    {
123        return $this->getDateTime()->format(self::getFormat());
124    }
125
126    /**
127     * Shortcut to {@link DateTime::format()}
128     * @param $string
129     * @return string
130     * @link https://php.net/manual/en/datetime.format.php
131     */
132    public function format($string)
133    {
134        return $this->getDateTime()->format($string);
135    }
136
137
138}
139