~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Helper/Date.php

  • Committer: Dan Garner
  • Date: 2016-02-15 17:54:45 UTC
  • mto: (454.4.130)
  • mto: This revision was merged to the branch mainline in revision 484.
  • Revision ID: git-v1:dd226a6f84464ff28758a249e1fd52ca4a35d911
Correction to Layout Duration ToolTip
xibosignage/xibo#721

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * Xibo - Digital Signage - http://www.xibo.org.uk
 
4
 * Copyright (C) 2009-13 Daniel Garner
 
5
 *
 
6
 * This file is part of Xibo.
 
7
 *
 
8
 * Xibo is free software: you can redistribute it and/or modify
 
9
 * it under the terms of the GNU Affero General Public License as published by
 
10
 * the Free Software Foundation, either version 3 of the License, or
 
11
 * any later version. 
 
12
 *
 
13
 * Xibo is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU Affero General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU Affero General Public License
 
19
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
namespace Xibo\Helper;
 
22
use DateTime;
 
23
use jDateTime;
 
24
 
 
25
 
 
26
class Date
 
27
{
 
28
    private static $timezones = null;
 
29
 
 
30
    /**
 
31
     * Get a local date
 
32
     * @param int|\Jenssegers\Date\Date $timestamp
 
33
     * @param string $format
 
34
     * @return string
 
35
     */
 
36
    public static function getLocalDate($timestamp = NULL, $format = NULL)
 
37
    {
 
38
        if ($format == NULL)
 
39
            $format = Date::getSystemFormat();
 
40
 
 
41
        if ($timestamp instanceof \Jenssegers\Date\Date)
 
42
            return $timestamp->format($format);
 
43
 
 
44
        if ($timestamp == NULL)
 
45
            $timestamp = time();
 
46
 
 
47
        if (Date::getCalendarType() == 'Jalali') {
 
48
            return JDateTime::date($format, $timestamp, false);
 
49
        }
 
50
        else {
 
51
            return \Jenssegers\Date\Date::createFromTimestamp($timestamp)->format($format);
 
52
        }
 
53
    }
 
54
 
 
55
    /**
 
56
     * Get the Calendar Type
 
57
     * @return string
 
58
     */
 
59
    private static function getCalendarType()
 
60
    {
 
61
        return Config::GetSetting('CALENDAR_TYPE');
 
62
    }
 
63
 
 
64
    /**
 
65
     * Get the default date format
 
66
     * @return string
 
67
     */
 
68
    private static function getSystemFormat()
 
69
    {
 
70
        return 'Y-m-d H:i:s';
 
71
    }
 
72
 
 
73
    /**
 
74
     * Get Date from String
 
75
     * @param string $string
 
76
     * @param string $format
 
77
     * @return \Jenssegers\Date\Date
 
78
     */
 
79
    public static function parse($string = null, $format = null)
 
80
    {
 
81
        if ($string == null)
 
82
            $string = Date::getLocalDate();
 
83
 
 
84
        if ($format == null)
 
85
            $format = Date::getSystemFormat();
 
86
 
 
87
        if (Date::getCalendarType() == 'Jalali') {
 
88
            // If we are Jalali, then we want to convert from Jalali back to Gregorian.
 
89
            // Split the time stamp into its component parts and pass it to the conversion.
 
90
            $date = trim($string);
 
91
 
 
92
            $split = (stripos($date, ' ') > 0) ? explode(' ', $date) : array($date, '');
 
93
 
 
94
            $dateSplit = explode('-', $split[0]);
 
95
            $timeSplit = explode(':', $split[1]);
 
96
 
 
97
            $date = jDateTime::toGregorian($dateSplit[0], $dateSplit[1], $dateSplit[2]);
 
98
 
 
99
            // Create a date out of that string.
 
100
            return \Jenssegers\Date\Date::create($date[0], $date[1], $date[2], $timeSplit[0], $timeSplit[1]);
 
101
        }
 
102
        else {
 
103
            return \Jenssegers\Date\Date::createFromFormat($format, $string);
 
104
        }
 
105
    }
 
106
 
 
107
    /**
 
108
     * Converts a format to moment
 
109
     *  inspired by http://stackoverflow.com/questions/30186611/php-dateformat-to-moment-js-format
 
110
     * @param $format
 
111
     * @return string
 
112
     */
 
113
    public static function convertPhpToMomentFormat($format)
 
114
    {
 
115
        $replacements = [
 
116
            'd' => 'DD',
 
117
            'D' => 'ddd',
 
118
            'j' => 'D',
 
119
            'l' => 'dddd',
 
120
            'N' => 'E',
 
121
            'S' => 'o',
 
122
            'w' => 'e',
 
123
            'z' => 'DDD',
 
124
            'W' => 'W',
 
125
            'F' => 'MMMM',
 
126
            'm' => 'MM',
 
127
            'M' => 'MMM',
 
128
            'n' => 'M',
 
129
            't' => '', // no equivalent
 
130
            'L' => '', // no equivalent
 
131
            'o' => 'YYYY',
 
132
            'Y' => 'YYYY',
 
133
            'y' => 'YY',
 
134
            'a' => 'a',
 
135
            'A' => 'A',
 
136
            'B' => '', // no equivalent
 
137
            'g' => 'h',
 
138
            'G' => 'H',
 
139
            'h' => 'hh',
 
140
            'H' => 'HH',
 
141
            'i' => 'mm',
 
142
            's' => 'ss',
 
143
            'u' => 'SSS',
 
144
            'e' => 'zz', // deprecated since version 1.6.0 of moment.js
 
145
            'I' => '', // no equivalent
 
146
            'O' => '', // no equivalent
 
147
            'P' => '', // no equivalent
 
148
            'T' => '', // no equivalent
 
149
            'Z' => '', // no equivalent
 
150
            'c' => '', // no equivalent
 
151
            'r' => '', // no equivalent
 
152
            'U' => 'X',
 
153
        ];
 
154
        $momentFormat = strtr($format, $replacements);
 
155
        return $momentFormat;
 
156
    }
 
157
 
 
158
    /**
 
159
     * Converts a format to bootstrap date picker
 
160
     *  inspired by http://stackoverflow.com/questions/30186611/php-dateformat-to-moment-js-format
 
161
     * @param $format
 
162
     * @return string
 
163
     */
 
164
    public static function convertPhpToBootstrapFormat($format)
 
165
    {
 
166
        $replacements = [
 
167
            'd' => 'dd',
 
168
            'D' => '',
 
169
            'j' => 'd',
 
170
            'l' => '',
 
171
            'N' => '',
 
172
            'S' => '',
 
173
            'w' => '',
 
174
            'z' => '',
 
175
            'W' => '',
 
176
            'F' => 'MM',
 
177
            'm' => 'mm',
 
178
            'M' => 'M',
 
179
            'n' => 'i',
 
180
            't' => '', // no equivalent
 
181
            'L' => '', // no equivalent
 
182
            'o' => 'yyyy',
 
183
            'Y' => 'yyyy',
 
184
            'y' => 'yy',
 
185
            'a' => 'p',
 
186
            'A' => 'P',
 
187
            'B' => '', // no equivalent
 
188
            'g' => 'H',
 
189
            'G' => 'h',
 
190
            'h' => 'HH',
 
191
            'H' => 'hh',
 
192
            'i' => 'ii',
 
193
            's' => 'ss',
 
194
            'u' => '',
 
195
            'e' => '', // deprecated since version 1.6.0 of moment.js
 
196
            'I' => '', // no equivalent
 
197
            'O' => '', // no equivalent
 
198
            'P' => '', // no equivalent
 
199
            'T' => '', // no equivalent
 
200
            'Z' => '', // no equivalent
 
201
            'c' => '', // no equivalent
 
202
            'r' => '', // no equivalent
 
203
            'U' => '',
 
204
        ];
 
205
        $momentFormat = strtr($format, $replacements);
 
206
        return $momentFormat;
 
207
    }
 
208
 
 
209
    /**
 
210
     * Timezone identifiers
 
211
     * @return array
 
212
     */
 
213
    public static function timezoneList()
 
214
    {
 
215
        if (self::$timezones === null) {
 
216
            self::$timezones = [];
 
217
            $offsets = [];
 
218
            $now = new DateTime();
 
219
 
 
220
            foreach (\DateTimeZone::listIdentifiers() as $timezone) {
 
221
                $now->setTimezone(new \DateTimeZone($timezone));
 
222
                $offsets[] = $offset = $now->getOffset();
 
223
                self::$timezones[$timezone] = '(' . self::formatGmtOffset($offset) . ') ' . self::formatTimezoneName($timezone);
 
224
            }
 
225
 
 
226
            array_multisort($offsets, self::$timezones);
 
227
        }
 
228
 
 
229
        return self::$timezones;
 
230
    }
 
231
 
 
232
    private static function formatGmtOffset($offset) {
 
233
        $hours = intval($offset / 3600);
 
234
        $minutes = abs(intval($offset % 3600 / 60));
 
235
        return 'GMT' . ($offset ? sprintf('%+03d:%02d', $hours, $minutes) : '');
 
236
    }
 
237
 
 
238
    private static function formatTimezoneName($name) {
 
239
        $name = str_replace('/', ', ', $name);
 
240
        $name = str_replace('_', ' ', $name);
 
241
        $name = str_replace('St ', 'St. ', $name);
 
242
        return $name;
 
243
    }
 
244
}