3
* Xibo - Digital Signage - http://www.xibo.org.uk
4
* Copyright (C) 2009-13 Daniel Garner
6
* This file is part of Xibo.
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
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.
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/>.
21
namespace Xibo\Helper;
28
private static $timezones = null;
32
* @param int|\Jenssegers\Date\Date $timestamp
33
* @param string $format
36
public static function getLocalDate($timestamp = NULL, $format = NULL)
39
$format = Date::getSystemFormat();
41
if ($timestamp instanceof \Jenssegers\Date\Date)
42
return $timestamp->format($format);
44
if ($timestamp == NULL)
47
if (Date::getCalendarType() == 'Jalali') {
48
return JDateTime::date($format, $timestamp, false);
51
return \Jenssegers\Date\Date::createFromTimestamp($timestamp)->format($format);
56
* Get the Calendar Type
59
private static function getCalendarType()
61
return Config::GetSetting('CALENDAR_TYPE');
65
* Get the default date format
68
private static function getSystemFormat()
74
* Get Date from String
75
* @param string $string
76
* @param string $format
77
* @return \Jenssegers\Date\Date
79
public static function parse($string = null, $format = null)
82
$string = Date::getLocalDate();
85
$format = Date::getSystemFormat();
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);
92
$split = (stripos($date, ' ') > 0) ? explode(' ', $date) : array($date, '');
94
$dateSplit = explode('-', $split[0]);
95
$timeSplit = explode(':', $split[1]);
97
$date = jDateTime::toGregorian($dateSplit[0], $dateSplit[1], $dateSplit[2]);
99
// Create a date out of that string.
100
return \Jenssegers\Date\Date::create($date[0], $date[1], $date[2], $timeSplit[0], $timeSplit[1]);
103
return \Jenssegers\Date\Date::createFromFormat($format, $string);
108
* Converts a format to moment
109
* inspired by http://stackoverflow.com/questions/30186611/php-dateformat-to-moment-js-format
113
public static function convertPhpToMomentFormat($format)
129
't' => '', // no equivalent
130
'L' => '', // no equivalent
136
'B' => '', // no equivalent
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
154
$momentFormat = strtr($format, $replacements);
155
return $momentFormat;
159
* Converts a format to bootstrap date picker
160
* inspired by http://stackoverflow.com/questions/30186611/php-dateformat-to-moment-js-format
164
public static function convertPhpToBootstrapFormat($format)
180
't' => '', // no equivalent
181
'L' => '', // no equivalent
187
'B' => '', // no equivalent
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
205
$momentFormat = strtr($format, $replacements);
206
return $momentFormat;
210
* Timezone identifiers
213
public static function timezoneList()
215
if (self::$timezones === null) {
216
self::$timezones = [];
218
$now = new DateTime();
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);
226
array_multisort($offsets, self::$timezones);
229
return self::$timezones;
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) : '');
238
private static function formatTimezoneName($name) {
239
$name = str_replace('/', ', ', $name);
240
$name = str_replace('_', ' ', $name);
241
$name = str_replace('St ', 'St. ', $name);