blob: cb15f6df6b998e11f7ae59442cf022bf86ea9c32 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnesdc3e4be2011-12-19 13:48:29 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnesdc3e4be2011-12-19 13:48:29 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Date Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/date_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
43 * Get "now" time
44 *
45 * Returns time() or its GMT equivalent based on the config file preference
46 *
47 * @access public
48 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020049 */
Derek Allard2067d1a2008-11-13 22:59:24 +000050if ( ! function_exists('now'))
51{
52 function now()
53 {
54 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020055
Derek Allard2067d1a2008-11-13 22:59:24 +000056 if (strtolower($CI->config->item('time_reference')) == 'gmt')
57 {
58 $now = time();
59 $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 if (strlen($system_time) < 10)
62 {
63 $system_time = time();
64 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
65 }
Barry Mienydd671972010-10-04 16:33:58 +020066
Derek Allard2067d1a2008-11-13 22:59:24 +000067 return $system_time;
68 }
Greg Akerc964e722011-08-29 19:31:29 -050069
70 return time();
Derek Allard2067d1a2008-11-13 22:59:24 +000071 }
72}
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074// ------------------------------------------------------------------------
75
76/**
77 * Convert MySQL Style Datecodes
78 *
79 * This function is identical to PHPs date() function,
80 * except that it allows date codes to be formatted using
81 * the MySQL style, where each code letter is preceded
Derek Jones4b9c6292011-07-01 17:40:48 -050082 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000083 *
84 * The benefit of doing dates this way is that you don't
85 * have to worry about escaping your text letters that
86 * match the date codes.
87 *
88 * @access public
89 * @param string
90 * @param integer
91 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020092 */
Derek Allard2067d1a2008-11-13 22:59:24 +000093if ( ! function_exists('mdate'))
94{
95 function mdate($datestr = '', $time = '')
96 {
97 if ($datestr == '')
Greg Akerf9168392011-08-29 19:29:05 -050098 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -050099 return '';
Greg Akerf9168392011-08-29 19:29:05 -0500100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Greg Akerc964e722011-08-29 19:31:29 -0500102 $time = ($time == '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +0200103
Greg Akerf9168392011-08-29 19:29:05 -0500104 $datestr = str_replace(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500105 '%\\',
106 '',
Greg Akerf9168392011-08-29 19:29:05 -0500107 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
108 );
Greg Akerc964e722011-08-29 19:31:29 -0500109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 return date($datestr, $time);
111 }
112}
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114// ------------------------------------------------------------------------
115
116/**
117 * Standard Date
118 *
119 * Returns a date formatted according to the submitted standard.
120 *
121 * @access public
122 * @param string the chosen format
123 * @param integer Unix timestamp
124 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200125 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000126if ( ! function_exists('standard_date'))
127{
128 function standard_date($fmt = 'DATE_RFC822', $time = '')
129 {
130 $formats = array(
131 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
132 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Greg Aker3b889a92011-01-10 12:35:57 -0600133 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
Syahril Zulkefli90658ad2011-11-13 23:43:37 +0800135 'DATE_RFC850' => '%l, %d-%M-%y %H:%i:%s UTC',
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
137 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
138 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
139 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
140 );
141
142 if ( ! isset($formats[$fmt]))
143 {
144 return FALSE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 return mdate($formats[$fmt], $time);
148 }
149}
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151// ------------------------------------------------------------------------
152
153/**
154 * Timespan
155 *
156 * Returns a span of seconds in this format:
157 * 10 days 14 hours 36 minutes 47 seconds
158 *
159 * @access public
160 * @param integer a number of seconds
161 * @param integer Unix timestamp
162 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200163 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000164if ( ! function_exists('timespan'))
165{
166 function timespan($seconds = 1, $time = '')
167 {
168 $CI =& get_instance();
169 $CI->lang->load('date');
170
171 if ( ! is_numeric($seconds))
172 {
173 $seconds = 1;
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 if ( ! is_numeric($time))
177 {
178 $time = time();
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Greg Akerf9168392011-08-29 19:29:05 -0500181 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $str = '';
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500184 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200187 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200189 }
190
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500191 $seconds -= $years * 31557600;
192 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if ($years > 0 OR $months > 0)
195 {
196 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200197 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200199 }
200
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500201 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
203
204 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 if ($years > 0 OR $months > 0 OR $weeks > 0)
207 {
208 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200214 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 if ($months > 0 OR $weeks > 0 OR $days > 0)
219 {
220 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200221 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $seconds -= $days * 86400;
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if ($days > 0 OR $hours > 0)
231 {
232 if ($hours > 0)
233 {
234 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $seconds -= $hours * 3600;
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 if ($days > 0 OR $hours > 0 OR $minutes > 0)
243 {
244 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200245 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 $seconds -= $minutes * 60;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 if ($str == '')
253 {
254 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 return substr(trim($str), 0, -1);
258 }
259}
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261// ------------------------------------------------------------------------
262
263/**
264 * Number of days in a month
265 *
266 * Takes a month/year as input and returns the number of days
267 * for the given month/year. Takes leap years into consideration.
268 *
269 * @access public
270 * @param integer a numeric month
271 * @param integer a numeric year
272 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200273 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000274if ( ! function_exists('days_in_month'))
275{
276 function days_in_month($month = 0, $year = '')
277 {
278 if ($month < 1 OR $month > 12)
279 {
280 return 0;
281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if ( ! is_numeric($year) OR strlen($year) != 4)
284 {
285 $year = date('Y');
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ($month == 2)
289 {
290 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
291 {
292 return 29;
293 }
294 }
295
296 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
297 return $days_in_month[$month - 1];
298 }
299}
Derek Jones36591092010-03-05 10:05:51 -0600300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301// ------------------------------------------------------------------------
302
303/**
304 * Converts a local Unix timestamp to GMT
305 *
306 * @access public
307 * @param integer Unix timestamp
308 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200309 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000310if ( ! function_exists('local_to_gmt'))
311{
312 function local_to_gmt($time = '')
313 {
314 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500315 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500317 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500318
Greg Akerf9168392011-08-29 19:29:05 -0500319 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500320 gmdate("H", $time),
321 gmdate("i", $time),
322 gmdate("s", $time),
323 gmdate("m", $time),
324 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500325 gmdate("Y", $time)
326 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328}
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330// ------------------------------------------------------------------------
331
332/**
333 * Converts GMT time to a localized value
334 *
335 * Takes a Unix timestamp (in GMT) as input, and returns
336 * at the local value based on the timezone and DST setting
337 * submitted
338 *
339 * @access public
340 * @param integer Unix timestamp
341 * @param string timezone
342 * @param bool whether DST is active
343 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200344 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000345if ( ! function_exists('gmt_to_local'))
346{
347 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200348 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 if ($time == '')
350 {
351 return now();
352 }
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 $time += timezones($timezone) * 3600;
355
356 if ($dst == TRUE)
357 {
358 $time += 3600;
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 return $time;
362 }
363}
Derek Jones36591092010-03-05 10:05:51 -0600364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365// ------------------------------------------------------------------------
366
367/**
368 * Converts a MySQL Timestamp to Unix
369 *
370 * @access public
371 * @param integer Unix timestamp
372 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200373 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000374if ( ! function_exists('mysql_to_unix'))
375{
376 function mysql_to_unix($time = '')
377 {
378 // We'll remove certain characters for backward compatibility
379 // since the formatting changed with MySQL 4.1
380 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 $time = str_replace('-', '', $time);
383 $time = str_replace(':', '', $time);
384 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500387 return mktime(
388 substr($time, 8, 2),
389 substr($time, 10, 2),
390 substr($time, 12, 2),
391 substr($time, 4, 2),
392 substr($time, 6, 2),
393 substr($time, 0, 4)
394 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
396}
Barry Mienydd671972010-10-04 16:33:58 +0200397
Derek Allard2067d1a2008-11-13 22:59:24 +0000398// ------------------------------------------------------------------------
399
400/**
401 * Unix to "Human"
402 *
403 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
404 *
405 * @access public
406 * @param integer Unix timestamp
407 * @param bool whether to show seconds
408 * @param string format: us or euro
409 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200410 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000411if ( ! function_exists('unix_to_human'))
412{
413 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
414 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500415 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 if ($fmt == 'us')
418 {
419 $r .= date('h', $time).':'.date('i', $time);
420 }
421 else
422 {
423 $r .= date('H', $time).':'.date('i', $time);
424 }
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 if ($seconds)
427 {
428 $r .= ':'.date('s', $time);
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 if ($fmt == 'us')
432 {
433 $r .= ' '.date('A', $time);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 return $r;
437 }
438}
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440// ------------------------------------------------------------------------
441
442/**
443 * Convert "human" date to GMT
444 *
445 * Reverses the above process
446 *
447 * @access public
448 * @param string format: us or euro
449 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200450 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000451if ( ! function_exists('human_to_unix'))
452{
453 function human_to_unix($datestr = '')
454 {
455 if ($datestr == '')
456 {
457 return FALSE;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600461 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 if ( ! preg_match('/^[0-9]{2,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))
464 {
465 return FALSE;
466 }
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Jones36591092010-03-05 10:05:51 -0600468 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000469
470 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Jones4b9c6292011-07-01 17:40:48 -0500472 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
473 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
474 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
476 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500479 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000480
Derek Jones1322ec52009-03-11 17:01:14 +0000481 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500483 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485 else
486 {
487 // Unless specified, seconds get set to zero.
488 $sec = '00';
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 if (isset($split['2']))
492 {
493 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500496 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500497 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500501 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500502 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500503 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500506 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500507 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500508 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 return mktime($hour, $min, $sec, $month, $day, $year);
512 }
513}
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515// ------------------------------------------------------------------------
516
517/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300518 * Turns many "reasonably-date-like" strings into something
519 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500520 *
Kyle Farris896d95a2011-08-21 23:03:54 -0300521 * @access public
522 * @param string The terribly formatted date-like string
523 * @param string Date format to return (same as php date function)
524 * @return string
525 */
526if ( ! function_exists('nice_date'))
527{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500528 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 {
530 if (empty($bad_date))
531 {
532 return 'Unknown';
533 }
Greg Akerf9168392011-08-29 19:29:05 -0500534
Kyle Farris896d95a2011-08-21 23:03:54 -0300535 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500536 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300537 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500538 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300539 {
540 $year = substr($bad_date, 0, 4);
541 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500542 }
543 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 {
545 $month = substr($bad_date, 0, 2);
546 $year = substr($bad_date, 2, 4);
547 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548
Kyle Farris896d95a2011-08-21 23:03:54 -0300549 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500551
Kyle Farris896d95a2011-08-21 23:03:54 -0300552 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500553 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300554 {
555 $month = substr($bad_date, 0, 2);
556 $day = substr($bad_date, 2, 2);
557 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 return date($format, strtotime($month . '/01/' . $year));
560 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500561
Kyle Farris896d95a2011-08-21 23:03:54 -0300562 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
563 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500564 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300565 list($m, $d, $y) = explode('-', $bad_date);
566 return date($format, strtotime("{$y}-{$m}-{$d}"));
567 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500568
Kyle Farris896d95a2011-08-21 23:03:54 -0300569 // Any other kind of string, when converted into UNIX time,
570 // produces "0 seconds after epoc..." is probably bad...
571 // return "Invalid Date".
572 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500573 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300574 return "Invalid Date";
575 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500576
Kyle Farris896d95a2011-08-21 23:03:54 -0300577 // It's probably a valid-ish date format already
578 return date($format, strtotime($bad_date));
579 }
580}
581
582// ------------------------------------------------------------------------
583
584/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 * Timezone Menu
586 *
587 * Generates a drop-down menu of timezones.
588 *
589 * @access public
590 * @param string timezone
591 * @param string classname
592 * @param string menu name
593 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200594 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000595if ( ! function_exists('timezone_menu'))
596{
597 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
598 {
599 $CI =& get_instance();
600 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200601
Greg Akerc964e722011-08-29 19:31:29 -0500602 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000603
604 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 if ($class != '')
607 {
608 $menu .= ' class="'.$class.'"';
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 foreach (timezones() as $key => $val)
614 {
615 $selected = ($default == $key) ? " selected='selected'" : '';
616 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
617 }
618
619 $menu .= "</select>";
620
621 return $menu;
622 }
623}
Barry Mienydd671972010-10-04 16:33:58 +0200624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625// ------------------------------------------------------------------------
626
627/**
628 * Timezones
629 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500630 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 * for various other ones in this library
632 *
633 * @access public
634 * @param string timezone
635 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200636 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000637if ( ! function_exists('timezones'))
638{
639 function timezones($tz = '')
640 {
641 // Note: Don't change the order of these even though
642 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200643
644 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500645 'UM12' => -12,
646 'UM11' => -11,
647 'UM10' => -10,
648 'UM95' => -9.5,
649 'UM9' => -9,
650 'UM8' => -8,
651 'UM7' => -7,
652 'UM6' => -6,
653 'UM5' => -5,
654 'UM45' => -4.5,
655 'UM4' => -4,
656 'UM35' => -3.5,
657 'UM3' => -3,
658 'UM2' => -2,
659 'UM1' => -1,
660 'UTC' => 0,
661 'UP1' => +1,
662 'UP2' => +2,
663 'UP3' => +3,
664 'UP35' => +3.5,
665 'UP4' => +4,
666 'UP45' => +4.5,
667 'UP5' => +5,
668 'UP55' => +5.5,
669 'UP575' => +5.75,
670 'UP6' => +6,
671 'UP65' => +6.5,
672 'UP7' => +7,
673 'UP8' => +8,
674 'UP875' => +8.75,
675 'UP9' => +9,
676 'UP95' => +9.5,
677 'UP10' => +10,
678 'UP105' => +10.5,
679 'UP11' => +11,
680 'UP115' => +11.5,
681 'UP12' => +12,
682 'UP1275' => +12.75,
683 'UP13' => +13,
684 'UP14' => +14
685 );
Barry Mienydd671972010-10-04 16:33:58 +0200686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 if ($tz == '')
688 {
689 return $zones;
690 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500691
Greg Akerf9168392011-08-29 19:29:05 -0500692 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
695 }
696}
697
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200698// ------------------------------------------------------------------------
699
700/**
701 * Date range
702 *
703 * Returns a list of dates within a specified period.
704 *
705 * @access public
706 * @param int unix_start UNIX timestamp of period start date
707 * @param int unix_end|days UNIX timestamp of period end date
708 * or interval in days.
709 * @param mixed is_unix Specifies wether the second @param
710 * is a UNIX timestamp or day interval
711 * - TRUE or 'unix' for a timestamp
712 * - FALSE or 'days' for an interval
713 * @param string date_format Output date format, same as in date()
714 * @return array
715 */
716if ( ! function_exists('date_range'))
717{
718 function date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d')
719 {
720 if ($unix_start == '' OR $mixed == '' OR $format == '')
721 {
722 return FALSE;
723 }
724
725 $is_unix = ! ( ! $is_unix OR $is_unix === 'days');
726
727 // Validate input and try strtotime() on invalid timestamps/intervals, just in case
728 if ( ( ! preg_match('/^[0-9]+$/', $unix_start) && ($unix_start = @strtotime($unix_time)) === FALSE)
729 OR ( ! preg_match('/^[0-9]+$/', $mixed) && ($is_unix === FALSE OR ($mixed = @strtotime($mixed)) === FALSE))
730 OR ($is_unix === TRUE && $mixed < $unix_start))
731 {
732 return FALSE;
733 }
734
735 if ($is_unix && ($unix_start == $mixed OR date($format, $unix_start) === date($format, $mixed)))
736 {
737 return array($start_date);
738 }
739
740 $range = array();
741
Andrey Andreev30da39b2012-03-10 15:49:17 +0200742 /* NOTE: Even though the DateTime object has many useful features, it appears that
743 * it doesn't always handle properly timezones, when timestamps are passed
744 * directly to its constructor. Neither of the following gave proper results:
745 *
746 * new DateTime('<timestamp>')
747 * new DateTime('<timestamp>', '<timezone>')
748 *
749 * --- available in PHP 5.3:
750 *
751 * DateTime::createFromFormat('<format>', '<timestamp>')
752 * DateTime::createFromFormat('<format>', '<timestamp>', '<timezone')
753 *
754 * ... so we'll have to set the timestamp after the object is instantiated.
755 * Furthermore, in PHP 5.3 we can use DateTime::setTimestamp() to do that and
756 * given that we have UNIX timestamps - we should use it.
757 */
758 $from = new DateTime();
759
760 if (is_php('5.3'))
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200761 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200762 $from->setTimestamp($unix_start);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200763 if ($is_unix)
764 {
765 $arg = new DateTime();
Andrey Andreev30da39b2012-03-10 15:49:17 +0200766 $arg->setTimestamp($mixed);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200767 }
768 else
769 {
770 $arg = (int) $mixed;
771 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200772
Andrey Andreev30da39b2012-03-10 15:49:17 +0200773 $period = new DatePeriod($from, new DateInterval('P1D'), $arg);
774 foreach ($period as $date)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200775 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200776 $range[] = $date->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200777 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200778
Andrey Andreev30da39b2012-03-10 15:49:17 +0200779 /* If a period end date was passed to the DatePeriod constructor, it might not
780 * be in our results. Not sure if this is a bug or it's just possible because
781 * the end date might actually be less than 24 hours away from the previously
782 * generated DateTime object, but either way - we have to append it manually.
783 */
784 if ( ! is_int($arg) && $range[count($range) - 1] !== $arg->format($format))
785 {
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200786 $range[] = $arg->format($format);
787 }
788
789 return $range;
790 }
791
Andrey Andreev30da39b2012-03-10 15:49:17 +0200792 $from->setDate(date('Y', $unix_start), date('n', $unix_start), date('j', $unix_start));
793 $from->setTime(date('G', $unix_start), date('i', $unix_start), date('s', $unix_start));
794 if ($is_unix)
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200795 {
Andrey Andreev30da39b2012-03-10 15:49:17 +0200796 $arg = new DateTime();
797 $arg->setDate(date('Y', $mixed), date('n', $mixed), date('j', $mixed));
798 $arg->setTime(date('G', $mixed), date('i', $mixed), date('s', $mixed));
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200799 }
Andrey Andreev30da39b2012-03-10 15:49:17 +0200800 else
801 {
802 $arg = (int) $mixed;
803 }
804 $range[] = $from->format($format);
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200805
Andrey Andreev30da39b2012-03-10 15:49:17 +0200806 if (is_int($arg)) // Day intervals
807 {
808 do
809 {
810 $from->modify('+1 day');
811 $range[] = $from->format($format);
812 }
813 while (--$arg > 0);
814 }
815 else // end date UNIX timestamp
816 {
817 for ($from->modify('+1 day'), $end_check = $arg->format('Ymd'); $from->format('Ymd') < $end_check; $from->modify('+1 day'))
818 {
819 $range[] = $from->format($format);
820 }
821
822 // Our loop only appended dates prior to our end date
823 $range[] = $arg->format($format);
824 }
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200825
826 return $range;
827 }
828}
829
Derek Allard2067d1a2008-11-13 22:59:24 +0000830/* End of file date_helper.php */
Andrey Andreev2139ecd2012-01-11 23:58:50 +0200831/* Location: ./system/helpers/date_helper.php */