| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2414 |
lars |
9 |
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2414 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
|
|
|
33 |
* @license https://opensource.org/licenses/MIT MIT License
|
| 68 |
lars |
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 1.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CodeIgniter Date Helpers
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Helpers
|
|
|
45 |
* @category Helpers
|
|
|
46 |
* @author EllisLab Dev Team
|
|
|
47 |
* @link https://codeigniter.com/user_guide/helpers/date_helper.html
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
// ------------------------------------------------------------------------
|
|
|
51 |
|
|
|
52 |
if ( ! function_exists('now'))
|
|
|
53 |
{
|
|
|
54 |
/**
|
|
|
55 |
* Get "now" time
|
|
|
56 |
*
|
|
|
57 |
* Returns time() based on the timezone parameter or on the
|
|
|
58 |
* "time_reference" setting
|
|
|
59 |
*
|
|
|
60 |
* @param string
|
|
|
61 |
* @return int
|
|
|
62 |
*/
|
|
|
63 |
function now($timezone = NULL)
|
|
|
64 |
{
|
|
|
65 |
if (empty($timezone))
|
|
|
66 |
{
|
|
|
67 |
$timezone = config_item('time_reference');
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if ($timezone === 'local' OR $timezone === date_default_timezone_get())
|
|
|
71 |
{
|
|
|
72 |
return time();
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$datetime = new DateTime('now', new DateTimeZone($timezone));
|
|
|
76 |
sscanf($datetime->format('j-n-Y G:i:s'), '%d-%d-%d %d:%d:%d', $day, $month, $year, $hour, $minute, $second);
|
|
|
77 |
|
|
|
78 |
return mktime($hour, $minute, $second, $month, $day, $year);
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// ------------------------------------------------------------------------
|
|
|
83 |
|
|
|
84 |
if ( ! function_exists('mdate'))
|
|
|
85 |
{
|
|
|
86 |
/**
|
|
|
87 |
* Convert MySQL Style Datecodes
|
|
|
88 |
*
|
|
|
89 |
* This function is identical to PHPs date() function,
|
|
|
90 |
* except that it allows date codes to be formatted using
|
|
|
91 |
* the MySQL style, where each code letter is preceded
|
|
|
92 |
* with a percent sign: %Y %m %d etc...
|
|
|
93 |
*
|
|
|
94 |
* The benefit of doing dates this way is that you don't
|
|
|
95 |
* have to worry about escaping your text letters that
|
|
|
96 |
* match the date codes.
|
|
|
97 |
*
|
|
|
98 |
* @param string
|
|
|
99 |
* @param int
|
|
|
100 |
* @return int
|
|
|
101 |
*/
|
|
|
102 |
function mdate($datestr = '', $time = '')
|
|
|
103 |
{
|
|
|
104 |
if ($datestr === '')
|
|
|
105 |
{
|
|
|
106 |
return '';
|
|
|
107 |
}
|
|
|
108 |
elseif (empty($time))
|
|
|
109 |
{
|
|
|
110 |
$time = now();
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
$datestr = str_replace(
|
|
|
114 |
'%\\',
|
|
|
115 |
'',
|
|
|
116 |
preg_replace('/([a-z]+?){1}/i', '\\\\\\1', $datestr)
|
|
|
117 |
);
|
|
|
118 |
|
|
|
119 |
return date($datestr, $time);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
// ------------------------------------------------------------------------
|
|
|
124 |
|
|
|
125 |
if ( ! function_exists('standard_date'))
|
|
|
126 |
{
|
|
|
127 |
/**
|
|
|
128 |
* Standard Date
|
|
|
129 |
*
|
|
|
130 |
* Returns a date formatted according to the submitted standard.
|
|
|
131 |
*
|
|
|
132 |
* As of PHP 5.2, the DateTime extension provides constants that
|
|
|
133 |
* serve for the exact same purpose and are used with date().
|
|
|
134 |
*
|
|
|
135 |
* @todo Remove in version 3.1+.
|
|
|
136 |
* @deprecated 3.0.0 Use PHP's native date() instead.
|
|
|
137 |
* @link http://www.php.net/manual/en/class.datetime.php#datetime.constants.types
|
|
|
138 |
*
|
|
|
139 |
* @example date(DATE_RFC822, now()); // default
|
|
|
140 |
* @example date(DATE_W3C, $time); // a different format and time
|
|
|
141 |
*
|
|
|
142 |
* @param string $fmt = 'DATE_RFC822' the chosen format
|
|
|
143 |
* @param int $time = NULL Unix timestamp
|
|
|
144 |
* @return string
|
|
|
145 |
*/
|
|
|
146 |
function standard_date($fmt = 'DATE_RFC822', $time = NULL)
|
|
|
147 |
{
|
|
|
148 |
if (empty($time))
|
|
|
149 |
{
|
|
|
150 |
$time = now();
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// Procedural style pre-defined constants from the DateTime extension
|
|
|
154 |
if (strpos($fmt, 'DATE_') !== 0 OR defined($fmt) === FALSE)
|
|
|
155 |
{
|
|
|
156 |
return FALSE;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
return date(constant($fmt), $time);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// ------------------------------------------------------------------------
|
|
|
164 |
|
|
|
165 |
if ( ! function_exists('timespan'))
|
|
|
166 |
{
|
|
|
167 |
/**
|
|
|
168 |
* Timespan
|
|
|
169 |
*
|
|
|
170 |
* Returns a span of seconds in this format:
|
|
|
171 |
* 10 days 14 hours 36 minutes 47 seconds
|
|
|
172 |
*
|
|
|
173 |
* @param int a number of seconds
|
|
|
174 |
* @param int Unix timestamp
|
|
|
175 |
* @param int a number of display units
|
|
|
176 |
* @return string
|
|
|
177 |
*/
|
|
|
178 |
function timespan($seconds = 1, $time = '', $units = 7)
|
|
|
179 |
{
|
|
|
180 |
$CI =& get_instance();
|
|
|
181 |
$CI->lang->load('date');
|
|
|
182 |
|
|
|
183 |
is_numeric($seconds) OR $seconds = 1;
|
|
|
184 |
is_numeric($time) OR $time = time();
|
|
|
185 |
is_numeric($units) OR $units = 7;
|
|
|
186 |
|
|
|
187 |
$seconds = ($time <= $seconds) ? 1 : $time - $seconds;
|
|
|
188 |
|
|
|
189 |
$str = array();
|
|
|
190 |
$years = floor($seconds / 31557600);
|
|
|
191 |
|
|
|
192 |
if ($years > 0)
|
|
|
193 |
{
|
|
|
194 |
$str[] = $years.' '.$CI->lang->line($years > 1 ? 'date_years' : 'date_year');
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
$seconds -= $years * 31557600;
|
|
|
198 |
$months = floor($seconds / 2629743);
|
|
|
199 |
|
|
|
200 |
if (count($str) < $units && ($years > 0 OR $months > 0))
|
|
|
201 |
{
|
|
|
202 |
if ($months > 0)
|
|
|
203 |
{
|
|
|
204 |
$str[] = $months.' '.$CI->lang->line($months > 1 ? 'date_months' : 'date_month');
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
$seconds -= $months * 2629743;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
$weeks = floor($seconds / 604800);
|
|
|
211 |
|
|
|
212 |
if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
|
|
|
213 |
{
|
|
|
214 |
if ($weeks > 0)
|
|
|
215 |
{
|
|
|
216 |
$str[] = $weeks.' '.$CI->lang->line($weeks > 1 ? 'date_weeks' : 'date_week');
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
$seconds -= $weeks * 604800;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
$days = floor($seconds / 86400);
|
|
|
223 |
|
|
|
224 |
if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
|
|
|
225 |
{
|
|
|
226 |
if ($days > 0)
|
|
|
227 |
{
|
|
|
228 |
$str[] = $days.' '.$CI->lang->line($days > 1 ? 'date_days' : 'date_day');
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
$seconds -= $days * 86400;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
$hours = floor($seconds / 3600);
|
|
|
235 |
|
|
|
236 |
if (count($str) < $units && ($days > 0 OR $hours > 0))
|
|
|
237 |
{
|
|
|
238 |
if ($hours > 0)
|
|
|
239 |
{
|
|
|
240 |
$str[] = $hours.' '.$CI->lang->line($hours > 1 ? 'date_hours' : 'date_hour');
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
$seconds -= $hours * 3600;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
$minutes = floor($seconds / 60);
|
|
|
247 |
|
|
|
248 |
if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
|
|
|
249 |
{
|
|
|
250 |
if ($minutes > 0)
|
|
|
251 |
{
|
|
|
252 |
$str[] = $minutes.' '.$CI->lang->line($minutes > 1 ? 'date_minutes' : 'date_minute');
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
$seconds -= $minutes * 60;
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
if (count($str) === 0)
|
|
|
259 |
{
|
|
|
260 |
$str[] = $seconds.' '.$CI->lang->line($seconds > 1 ? 'date_seconds' : 'date_second');
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
return implode(', ', $str);
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
// ------------------------------------------------------------------------
|
|
|
268 |
|
|
|
269 |
if ( ! function_exists('days_in_month'))
|
|
|
270 |
{
|
|
|
271 |
/**
|
|
|
272 |
* Number of days in a month
|
|
|
273 |
*
|
|
|
274 |
* Takes a month/year as input and returns the number of days
|
|
|
275 |
* for the given month/year. Takes leap years into consideration.
|
|
|
276 |
*
|
|
|
277 |
* @param int a numeric month
|
|
|
278 |
* @param int a numeric year
|
|
|
279 |
* @return int
|
|
|
280 |
*/
|
|
|
281 |
function days_in_month($month = 0, $year = '')
|
|
|
282 |
{
|
|
|
283 |
if ($month < 1 OR $month > 12)
|
|
|
284 |
{
|
|
|
285 |
return 0;
|
|
|
286 |
}
|
|
|
287 |
elseif ( ! is_numeric($year) OR strlen($year) !== 4)
|
|
|
288 |
{
|
|
|
289 |
$year = date('Y');
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
if (defined('CAL_GREGORIAN'))
|
|
|
293 |
{
|
|
|
294 |
return cal_days_in_month(CAL_GREGORIAN, $month, $year);
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
if ($year >= 1970)
|
|
|
298 |
{
|
|
|
299 |
return (int) date('t', mktime(12, 0, 0, $month, 1, $year));
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
if ($month == 2)
|
|
|
303 |
{
|
|
|
304 |
if ($year % 400 === 0 OR ($year % 4 === 0 && $year % 100 !== 0))
|
|
|
305 |
{
|
|
|
306 |
return 29;
|
|
|
307 |
}
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
$days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
|
|
|
311 |
return $days_in_month[$month - 1];
|
|
|
312 |
}
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
// ------------------------------------------------------------------------
|
|
|
316 |
|
|
|
317 |
if ( ! function_exists('local_to_gmt'))
|
|
|
318 |
{
|
|
|
319 |
/**
|
|
|
320 |
* Converts a local Unix timestamp to GMT
|
|
|
321 |
*
|
|
|
322 |
* @param int Unix timestamp
|
|
|
323 |
* @return int
|
|
|
324 |
*/
|
|
|
325 |
function local_to_gmt($time = '')
|
|
|
326 |
{
|
|
|
327 |
if ($time === '')
|
|
|
328 |
{
|
|
|
329 |
$time = time();
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
return mktime(
|
|
|
333 |
gmdate('G', $time),
|
|
|
334 |
gmdate('i', $time),
|
|
|
335 |
gmdate('s', $time),
|
|
|
336 |
gmdate('n', $time),
|
|
|
337 |
gmdate('j', $time),
|
|
|
338 |
gmdate('Y', $time)
|
|
|
339 |
);
|
|
|
340 |
}
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
// ------------------------------------------------------------------------
|
|
|
344 |
|
|
|
345 |
if ( ! function_exists('gmt_to_local'))
|
|
|
346 |
{
|
|
|
347 |
/**
|
|
|
348 |
* Converts GMT time to a localized value
|
|
|
349 |
*
|
|
|
350 |
* Takes a Unix timestamp (in GMT) as input, and returns
|
|
|
351 |
* at the local value based on the timezone and DST setting
|
|
|
352 |
* submitted
|
|
|
353 |
*
|
|
|
354 |
* @param int Unix timestamp
|
|
|
355 |
* @param string timezone
|
|
|
356 |
* @param bool whether DST is active
|
|
|
357 |
* @return int
|
|
|
358 |
*/
|
|
|
359 |
function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
|
|
|
360 |
{
|
|
|
361 |
if ($time === '')
|
|
|
362 |
{
|
|
|
363 |
return now();
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
$time += timezones($timezone) * 3600;
|
|
|
367 |
|
|
|
368 |
return ($dst === TRUE) ? $time + 3600 : $time;
|
|
|
369 |
}
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
// ------------------------------------------------------------------------
|
|
|
373 |
|
|
|
374 |
if ( ! function_exists('mysql_to_unix'))
|
|
|
375 |
{
|
|
|
376 |
/**
|
|
|
377 |
* Converts a MySQL Timestamp to Unix
|
|
|
378 |
*
|
|
|
379 |
* @param int MySQL timestamp YYYY-MM-DD HH:MM:SS
|
|
|
380 |
* @return int Unix timstamp
|
|
|
381 |
*/
|
|
|
382 |
function mysql_to_unix($time = '')
|
|
|
383 |
{
|
|
|
384 |
// We'll remove certain characters for backward compatibility
|
|
|
385 |
// since the formatting changed with MySQL 4.1
|
|
|
386 |
// YYYY-MM-DD HH:MM:SS
|
|
|
387 |
|
|
|
388 |
$time = str_replace(array('-', ':', ' '), '', $time);
|
|
|
389 |
|
|
|
390 |
// YYYYMMDDHHMMSS
|
|
|
391 |
return mktime(
|
|
|
392 |
substr($time, 8, 2),
|
|
|
393 |
substr($time, 10, 2),
|
|
|
394 |
substr($time, 12, 2),
|
|
|
395 |
substr($time, 4, 2),
|
|
|
396 |
substr($time, 6, 2),
|
|
|
397 |
substr($time, 0, 4)
|
|
|
398 |
);
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
// ------------------------------------------------------------------------
|
|
|
403 |
|
|
|
404 |
if ( ! function_exists('unix_to_human'))
|
|
|
405 |
{
|
|
|
406 |
/**
|
|
|
407 |
* Unix to "Human"
|
|
|
408 |
*
|
|
|
409 |
* Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
|
|
|
410 |
*
|
|
|
411 |
* @param int Unix timestamp
|
|
|
412 |
* @param bool whether to show seconds
|
|
|
413 |
* @param string format: us or euro
|
|
|
414 |
* @return string
|
|
|
415 |
*/
|
|
|
416 |
function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
|
|
|
417 |
{
|
|
|
418 |
$r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
|
|
|
419 |
|
|
|
420 |
if ($fmt === 'us')
|
|
|
421 |
{
|
|
|
422 |
$r .= date('h', $time).':'.date('i', $time);
|
|
|
423 |
}
|
|
|
424 |
else
|
|
|
425 |
{
|
|
|
426 |
$r .= date('H', $time).':'.date('i', $time);
|
|
|
427 |
}
|
|
|
428 |
|
|
|
429 |
if ($seconds)
|
|
|
430 |
{
|
|
|
431 |
$r .= ':'.date('s', $time);
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
if ($fmt === 'us')
|
|
|
435 |
{
|
|
|
436 |
return $r.' '.date('A', $time);
|
|
|
437 |
}
|
|
|
438 |
|
|
|
439 |
return $r;
|
|
|
440 |
}
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
// ------------------------------------------------------------------------
|
|
|
444 |
|
|
|
445 |
if ( ! function_exists('human_to_unix'))
|
|
|
446 |
{
|
|
|
447 |
/**
|
|
|
448 |
* Convert "human" date to GMT
|
|
|
449 |
*
|
|
|
450 |
* Reverses the above process
|
|
|
451 |
*
|
|
|
452 |
* @param string format: us or euro
|
|
|
453 |
* @return int
|
|
|
454 |
*/
|
|
|
455 |
function human_to_unix($datestr = '')
|
|
|
456 |
{
|
|
|
457 |
if ($datestr === '')
|
|
|
458 |
{
|
|
|
459 |
return FALSE;
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
$datestr = preg_replace('/\040+/', ' ', trim($datestr));
|
|
|
463 |
|
|
|
464 |
if ( ! preg_match('/^(\d{2}|\d{4})\-[0-9]{1,2}\-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?(?:\s[AP]M)?$/i', $datestr))
|
|
|
465 |
{
|
|
|
466 |
return FALSE;
|
|
|
467 |
}
|
|
|
468 |
|
|
|
469 |
sscanf($datestr, '%d-%d-%d %s %s', $year, $month, $day, $time, $ampm);
|
|
|
470 |
sscanf($time, '%d:%d:%d', $hour, $min, $sec);
|
|
|
471 |
isset($sec) OR $sec = 0;
|
|
|
472 |
|
|
|
473 |
if (isset($ampm))
|
|
|
474 |
{
|
|
|
475 |
$ampm = strtolower($ampm);
|
|
|
476 |
|
|
|
477 |
if ($ampm[0] === 'p' && $hour < 12)
|
|
|
478 |
{
|
|
|
479 |
$hour += 12;
|
|
|
480 |
}
|
|
|
481 |
elseif ($ampm[0] === 'a' && $hour === 12)
|
|
|
482 |
{
|
|
|
483 |
$hour = 0;
|
|
|
484 |
}
|
|
|
485 |
}
|
|
|
486 |
|
|
|
487 |
return mktime($hour, $min, $sec, $month, $day, $year);
|
|
|
488 |
}
|
|
|
489 |
}
|
|
|
490 |
|
|
|
491 |
// ------------------------------------------------------------------------
|
|
|
492 |
|
|
|
493 |
if ( ! function_exists('nice_date'))
|
|
|
494 |
{
|
|
|
495 |
/**
|
|
|
496 |
* Turns many "reasonably-date-like" strings into something
|
|
|
497 |
* that is actually useful. This only works for dates after unix epoch.
|
|
|
498 |
*
|
| 2049 |
lars |
499 |
* @deprecated 3.1.3 Use DateTime::createFromFormat($input_format, $input)->format($output_format);
|
| 68 |
lars |
500 |
* @param string The terribly formatted date-like string
|
|
|
501 |
* @param string Date format to return (same as php date function)
|
|
|
502 |
* @return string
|
|
|
503 |
*/
|
|
|
504 |
function nice_date($bad_date = '', $format = FALSE)
|
|
|
505 |
{
|
|
|
506 |
if (empty($bad_date))
|
|
|
507 |
{
|
|
|
508 |
return 'Unknown';
|
|
|
509 |
}
|
|
|
510 |
elseif (empty($format))
|
|
|
511 |
{
|
|
|
512 |
$format = 'U';
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
// Date like: YYYYMM
|
|
|
516 |
if (preg_match('/^\d{6}$/i', $bad_date))
|
|
|
517 |
{
|
|
|
518 |
if (in_array(substr($bad_date, 0, 2), array('19', '20')))
|
|
|
519 |
{
|
|
|
520 |
$year = substr($bad_date, 0, 4);
|
|
|
521 |
$month = substr($bad_date, 4, 2);
|
|
|
522 |
}
|
|
|
523 |
else
|
|
|
524 |
{
|
|
|
525 |
$month = substr($bad_date, 0, 2);
|
|
|
526 |
$year = substr($bad_date, 2, 4);
|
|
|
527 |
}
|
|
|
528 |
|
|
|
529 |
return date($format, strtotime($year.'-'.$month.'-01'));
|
|
|
530 |
}
|
|
|
531 |
|
|
|
532 |
// Date Like: YYYYMMDD
|
| 2049 |
lars |
533 |
if (preg_match('/^\d{8}$/i', $bad_date, $matches))
|
| 68 |
lars |
534 |
{
|
| 2049 |
lars |
535 |
return DateTime::createFromFormat('Ymd', $bad_date)->format($format);
|
| 68 |
lars |
536 |
}
|
|
|
537 |
|
|
|
538 |
// Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
|
|
|
539 |
if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/i', $bad_date, $matches))
|
|
|
540 |
{
|
|
|
541 |
return date($format, strtotime($matches[3].'-'.$matches[1].'-'.$matches[2]));
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
// Any other kind of string, when converted into UNIX time,
|
|
|
545 |
// produces "0 seconds after epoc..." is probably bad...
|
|
|
546 |
// return "Invalid Date".
|
|
|
547 |
if (date('U', strtotime($bad_date)) === '0')
|
|
|
548 |
{
|
|
|
549 |
return 'Invalid Date';
|
|
|
550 |
}
|
|
|
551 |
|
|
|
552 |
// It's probably a valid-ish date format already
|
|
|
553 |
return date($format, strtotime($bad_date));
|
|
|
554 |
}
|
|
|
555 |
}
|
|
|
556 |
|
|
|
557 |
// ------------------------------------------------------------------------
|
|
|
558 |
|
|
|
559 |
if ( ! function_exists('timezone_menu'))
|
|
|
560 |
{
|
|
|
561 |
/**
|
|
|
562 |
* Timezone Menu
|
|
|
563 |
*
|
|
|
564 |
* Generates a drop-down menu of timezones.
|
|
|
565 |
*
|
|
|
566 |
* @param string timezone
|
|
|
567 |
* @param string classname
|
|
|
568 |
* @param string menu name
|
|
|
569 |
* @param mixed attributes
|
|
|
570 |
* @return string
|
|
|
571 |
*/
|
|
|
572 |
function timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '')
|
|
|
573 |
{
|
|
|
574 |
$CI =& get_instance();
|
|
|
575 |
$CI->lang->load('date');
|
|
|
576 |
|
|
|
577 |
$default = ($default === 'GMT') ? 'UTC' : $default;
|
|
|
578 |
|
|
|
579 |
$menu = '<select name="'.$name.'"';
|
|
|
580 |
|
|
|
581 |
if ($class !== '')
|
|
|
582 |
{
|
|
|
583 |
$menu .= ' class="'.$class.'"';
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
$menu .= _stringify_attributes($attributes).">\n";
|
|
|
587 |
|
|
|
588 |
foreach (timezones() as $key => $val)
|
|
|
589 |
{
|
|
|
590 |
$selected = ($default === $key) ? ' selected="selected"' : '';
|
|
|
591 |
$menu .= '<option value="'.$key.'"'.$selected.'>'.$CI->lang->line($key)."</option>\n";
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
return $menu.'</select>';
|
|
|
595 |
}
|
|
|
596 |
}
|
|
|
597 |
|
|
|
598 |
// ------------------------------------------------------------------------
|
|
|
599 |
|
|
|
600 |
if ( ! function_exists('timezones'))
|
|
|
601 |
{
|
|
|
602 |
/**
|
|
|
603 |
* Timezones
|
|
|
604 |
*
|
|
|
605 |
* Returns an array of timezones. This is a helper function
|
|
|
606 |
* for various other ones in this library
|
|
|
607 |
*
|
|
|
608 |
* @param string timezone
|
|
|
609 |
* @return string
|
|
|
610 |
*/
|
|
|
611 |
function timezones($tz = '')
|
|
|
612 |
{
|
|
|
613 |
// Note: Don't change the order of these even though
|
|
|
614 |
// some items appear to be in the wrong order
|
|
|
615 |
|
|
|
616 |
$zones = array(
|
|
|
617 |
'UM12' => -12,
|
|
|
618 |
'UM11' => -11,
|
|
|
619 |
'UM10' => -10,
|
|
|
620 |
'UM95' => -9.5,
|
|
|
621 |
'UM9' => -9,
|
|
|
622 |
'UM8' => -8,
|
|
|
623 |
'UM7' => -7,
|
|
|
624 |
'UM6' => -6,
|
|
|
625 |
'UM5' => -5,
|
|
|
626 |
'UM45' => -4.5,
|
|
|
627 |
'UM4' => -4,
|
|
|
628 |
'UM35' => -3.5,
|
|
|
629 |
'UM3' => -3,
|
|
|
630 |
'UM2' => -2,
|
|
|
631 |
'UM1' => -1,
|
|
|
632 |
'UTC' => 0,
|
|
|
633 |
'UP1' => +1,
|
|
|
634 |
'UP2' => +2,
|
|
|
635 |
'UP3' => +3,
|
|
|
636 |
'UP35' => +3.5,
|
|
|
637 |
'UP4' => +4,
|
|
|
638 |
'UP45' => +4.5,
|
|
|
639 |
'UP5' => +5,
|
|
|
640 |
'UP55' => +5.5,
|
|
|
641 |
'UP575' => +5.75,
|
|
|
642 |
'UP6' => +6,
|
|
|
643 |
'UP65' => +6.5,
|
|
|
644 |
'UP7' => +7,
|
|
|
645 |
'UP8' => +8,
|
|
|
646 |
'UP875' => +8.75,
|
|
|
647 |
'UP9' => +9,
|
|
|
648 |
'UP95' => +9.5,
|
|
|
649 |
'UP10' => +10,
|
|
|
650 |
'UP105' => +10.5,
|
|
|
651 |
'UP11' => +11,
|
|
|
652 |
'UP115' => +11.5,
|
|
|
653 |
'UP12' => +12,
|
|
|
654 |
'UP1275' => +12.75,
|
|
|
655 |
'UP13' => +13,
|
|
|
656 |
'UP14' => +14
|
|
|
657 |
);
|
|
|
658 |
|
|
|
659 |
if ($tz === '')
|
|
|
660 |
{
|
|
|
661 |
return $zones;
|
|
|
662 |
}
|
|
|
663 |
|
|
|
664 |
return isset($zones[$tz]) ? $zones[$tz] : 0;
|
|
|
665 |
}
|
|
|
666 |
}
|
|
|
667 |
|
|
|
668 |
// ------------------------------------------------------------------------
|
|
|
669 |
|
|
|
670 |
if ( ! function_exists('date_range'))
|
|
|
671 |
{
|
|
|
672 |
/**
|
|
|
673 |
* Date range
|
|
|
674 |
*
|
|
|
675 |
* Returns a list of dates within a specified period.
|
|
|
676 |
*
|
|
|
677 |
* @param int unix_start UNIX timestamp of period start date
|
|
|
678 |
* @param int unix_end|days UNIX timestamp of period end date
|
|
|
679 |
* or interval in days.
|
|
|
680 |
* @param mixed is_unix Specifies whether the second parameter
|
|
|
681 |
* is a UNIX timestamp or a day interval
|
|
|
682 |
* - TRUE or 'unix' for a timestamp
|
|
|
683 |
* - FALSE or 'days' for an interval
|
|
|
684 |
* @param string date_format Output date format, same as in date()
|
|
|
685 |
* @return array
|
|
|
686 |
*/
|
|
|
687 |
function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d')
|
|
|
688 |
{
|
|
|
689 |
if ($unix_start == '' OR $mixed == '' OR $format == '')
|
|
|
690 |
{
|
|
|
691 |
return FALSE;
|
|
|
692 |
}
|
|
|
693 |
|
|
|
694 |
$is_unix = ! ( ! $is_unix OR $is_unix === 'days');
|
|
|
695 |
|
|
|
696 |
// Validate input and try strtotime() on invalid timestamps/intervals, just in case
|
|
|
697 |
if ( ( ! ctype_digit((string) $unix_start) && ($unix_start = @strtotime($unix_start)) === FALSE)
|
|
|
698 |
OR ( ! ctype_digit((string) $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
|
|
|
699 |
OR ($is_unix === TRUE && $mixed < $unix_start))
|
|
|
700 |
{
|
|
|
701 |
return FALSE;
|
|
|
702 |
}
|
|
|
703 |
|
|
|
704 |
if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed)))
|
|
|
705 |
{
|
|
|
706 |
return array(date($format, $unix_start));
|
|
|
707 |
}
|
|
|
708 |
|
|
|
709 |
$range = array();
|
|
|
710 |
|
|
|
711 |
$from = new DateTime();
|
| 1257 |
lars |
712 |
$from->setTimestamp($unix_start);
|
| 68 |
lars |
713 |
|
|
|
714 |
if ($is_unix)
|
|
|
715 |
{
|
|
|
716 |
$arg = new DateTime();
|
| 1257 |
lars |
717 |
$arg->setTimestamp($mixed);
|
| 68 |
lars |
718 |
}
|
|
|
719 |
else
|
|
|
720 |
{
|
|
|
721 |
$arg = (int) $mixed;
|
|
|
722 |
}
|
|
|
723 |
|
| 1257 |
lars |
724 |
$period = new DatePeriod($from, new DateInterval('P1D'), $arg);
|
|
|
725 |
foreach ($period as $date)
|
| 68 |
lars |
726 |
{
|
| 1257 |
lars |
727 |
$range[] = $date->format($format);
|
| 68 |
lars |
728 |
}
|
| 1257 |
lars |
729 |
|
|
|
730 |
/* If a period end date was passed to the DatePeriod constructor, it might not
|
|
|
731 |
* be in our results. Not sure if this is a bug or it's just possible because
|
|
|
732 |
* the end date might actually be less than 24 hours away from the previously
|
|
|
733 |
* generated DateTime object, but either way - we have to append it manually.
|
|
|
734 |
*/
|
|
|
735 |
if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format))
|
| 68 |
lars |
736 |
{
|
|
|
737 |
$range[] = $arg->format($format);
|
|
|
738 |
}
|
|
|
739 |
|
|
|
740 |
return $range;
|
|
|
741 |
}
|
|
|
742 |
}
|