blob: 70b01e348d5e2fedff7ab16e99723e7bc88971e1 [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{
Roger Herbert04c146d2012-03-11 15:31:01 +0000166 function timespan($seconds = 1, $time = '', $units = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
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
Roger Herbert04c146d2012-03-11 15:31:01 +0000181 if ( ! is_numeric($units))
182 {
183 $units = 999;
184 }
185
Roger Herbert11f46f72012-03-11 15:11:44 +0000186 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200187
Roger Herbert04c146d2012-03-11 15:31:01 +0000188 $str = array();
Roger Herbert11f46f72012-03-11 15:11:44 +0000189 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000193 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200194 }
195
Roger Herbert11f46f72012-03-11 15:11:44 +0000196 $seconds -= $years * 31557600;
197 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200198
Roger Herbert04c146d2012-03-11 15:31:01 +0000199 if (count($str) < $units AND ($years > 0 OR $months > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000200 {
201 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200202 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000203 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000204 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000205
206 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 }
208
Roger Herbert11f46f72012-03-11 15:11:44 +0000209 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200210
Roger Herbert04c146d2012-03-11 15:31:01 +0000211 if (count($str) < $units AND ($years > 0 OR $months > 0 OR $weeks > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000212 {
213 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200214 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000215 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000216 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000217
218 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200219 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
Roger Herbert11f46f72012-03-11 15:11:44 +0000221 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Roger Herbert04c146d2012-03-11 15:31:01 +0000223 if (count($str) < $units AND ($months > 0 OR $weeks > 0 OR $days > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000224 {
225 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200226 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000227 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000228 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000229
230 $seconds -= $days * 86400;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Roger Herbert11f46f72012-03-11 15:11:44 +0000233 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Roger Herbert04c146d2012-03-11 15:31:01 +0000235 if (count($str) < $units AND ($days > 0 OR $hours > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000236 {
237 if ($hours > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000239 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000240 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000241
242 $seconds -= $hours * 3600;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Roger Herbert11f46f72012-03-11 15:11:44 +0000245 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200246
Roger Herbert04c146d2012-03-11 15:31:01 +0000247 if (count($str) < $units AND ($days > 0 OR $hours > 0 OR $minutes > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000248 {
249 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200250 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000251 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000253
254 $seconds -= $minutes * 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Roger Herbert11f46f72012-03-11 15:11:44 +0000257 if ($str == '')
258 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000259 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Roger Herbert04c146d2012-03-11 15:31:01 +0000262 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264}
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266// ------------------------------------------------------------------------
267
268/**
269 * Number of days in a month
270 *
271 * Takes a month/year as input and returns the number of days
272 * for the given month/year. Takes leap years into consideration.
273 *
274 * @access public
275 * @param integer a numeric month
276 * @param integer a numeric year
277 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200278 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000279if ( ! function_exists('days_in_month'))
280{
281 function days_in_month($month = 0, $year = '')
282 {
283 if ($month < 1 OR $month > 12)
284 {
285 return 0;
286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ( ! is_numeric($year) OR strlen($year) != 4)
289 {
290 $year = date('Y');
291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 if ($month == 2)
294 {
295 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
296 {
297 return 29;
298 }
299 }
300
301 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
302 return $days_in_month[$month - 1];
303 }
304}
Derek Jones36591092010-03-05 10:05:51 -0600305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306// ------------------------------------------------------------------------
307
308/**
309 * Converts a local Unix timestamp to GMT
310 *
311 * @access public
312 * @param integer Unix timestamp
313 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000315if ( ! function_exists('local_to_gmt'))
316{
317 function local_to_gmt($time = '')
318 {
319 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500320 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500322 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500323
Greg Akerf9168392011-08-29 19:29:05 -0500324 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500325 gmdate("H", $time),
326 gmdate("i", $time),
327 gmdate("s", $time),
328 gmdate("m", $time),
329 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500330 gmdate("Y", $time)
331 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
333}
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335// ------------------------------------------------------------------------
336
337/**
338 * Converts GMT time to a localized value
339 *
340 * Takes a Unix timestamp (in GMT) as input, and returns
341 * at the local value based on the timezone and DST setting
342 * submitted
343 *
344 * @access public
345 * @param integer Unix timestamp
346 * @param string timezone
347 * @param bool whether DST is active
348 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200349 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000350if ( ! function_exists('gmt_to_local'))
351{
352 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200353 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 if ($time == '')
355 {
356 return now();
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 $time += timezones($timezone) * 3600;
360
361 if ($dst == TRUE)
362 {
363 $time += 3600;
364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 return $time;
367 }
368}
Derek Jones36591092010-03-05 10:05:51 -0600369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370// ------------------------------------------------------------------------
371
372/**
373 * Converts a MySQL Timestamp to Unix
374 *
375 * @access public
376 * @param integer Unix timestamp
377 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200378 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000379if ( ! function_exists('mysql_to_unix'))
380{
381 function mysql_to_unix($time = '')
382 {
383 // We'll remove certain characters for backward compatibility
384 // since the formatting changed with MySQL 4.1
385 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 $time = str_replace('-', '', $time);
388 $time = str_replace(':', '', $time);
389 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500392 return mktime(
393 substr($time, 8, 2),
394 substr($time, 10, 2),
395 substr($time, 12, 2),
396 substr($time, 4, 2),
397 substr($time, 6, 2),
398 substr($time, 0, 4)
399 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401}
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403// ------------------------------------------------------------------------
404
405/**
406 * Unix to "Human"
407 *
408 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
409 *
410 * @access public
411 * @param integer Unix timestamp
412 * @param bool whether to show seconds
413 * @param string format: us or euro
414 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200415 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000416if ( ! function_exists('unix_to_human'))
417{
418 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
419 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500420 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 if ($fmt == 'us')
423 {
424 $r .= date('h', $time).':'.date('i', $time);
425 }
426 else
427 {
428 $r .= date('H', $time).':'.date('i', $time);
429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 if ($seconds)
432 {
433 $r .= ':'.date('s', $time);
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 if ($fmt == 'us')
437 {
438 $r .= ' '.date('A', $time);
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 return $r;
442 }
443}
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445// ------------------------------------------------------------------------
446
447/**
448 * Convert "human" date to GMT
449 *
450 * Reverses the above process
451 *
452 * @access public
453 * @param string format: us or euro
454 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200455 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000456if ( ! function_exists('human_to_unix'))
457{
458 function human_to_unix($datestr = '')
459 {
460 if ($datestr == '')
461 {
462 return FALSE;
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600466 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 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))
469 {
470 return FALSE;
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Jones36591092010-03-05 10:05:51 -0600473 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000474
475 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Jones4b9c6292011-07-01 17:40:48 -0500477 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
478 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
479 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000480
481 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500484 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000485
Derek Jones1322ec52009-03-11 17:01:14 +0000486 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500488 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490 else
491 {
492 // Unless specified, seconds get set to zero.
493 $sec = '00';
494 }
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 if (isset($split['2']))
497 {
498 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500501 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500502 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500506 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500507 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500508 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500511 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500512 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500513 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 return mktime($hour, $min, $sec, $month, $day, $year);
517 }
518}
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520// ------------------------------------------------------------------------
521
522/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 * Turns many "reasonably-date-like" strings into something
524 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500525 *
Kyle Farris896d95a2011-08-21 23:03:54 -0300526 * @access public
527 * @param string The terribly formatted date-like string
528 * @param string Date format to return (same as php date function)
529 * @return string
530 */
531if ( ! function_exists('nice_date'))
532{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500533 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300534 {
535 if (empty($bad_date))
536 {
537 return 'Unknown';
538 }
Greg Akerf9168392011-08-29 19:29:05 -0500539
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500541 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 {
545 $year = substr($bad_date, 0, 4);
546 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500547 }
548 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300549 {
550 $month = substr($bad_date, 0, 2);
551 $year = substr($bad_date, 2, 4);
552 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500553
Kyle Farris896d95a2011-08-21 23:03:54 -0300554 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300555 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500556
Kyle Farris896d95a2011-08-21 23:03:54 -0300557 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 {
560 $month = substr($bad_date, 0, 2);
561 $day = substr($bad_date, 2, 2);
562 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500563
Kyle Farris896d95a2011-08-21 23:03:54 -0300564 return date($format, strtotime($month . '/01/' . $year));
565 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500566
Kyle Farris896d95a2011-08-21 23:03:54 -0300567 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
568 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500569 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300570 list($m, $d, $y) = explode('-', $bad_date);
571 return date($format, strtotime("{$y}-{$m}-{$d}"));
572 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500573
Kyle Farris896d95a2011-08-21 23:03:54 -0300574 // Any other kind of string, when converted into UNIX time,
575 // produces "0 seconds after epoc..." is probably bad...
576 // return "Invalid Date".
577 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500578 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300579 return "Invalid Date";
580 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500581
Kyle Farris896d95a2011-08-21 23:03:54 -0300582 // It's probably a valid-ish date format already
583 return date($format, strtotime($bad_date));
584 }
585}
586
587// ------------------------------------------------------------------------
588
589/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 * Timezone Menu
591 *
592 * Generates a drop-down menu of timezones.
593 *
594 * @access public
595 * @param string timezone
596 * @param string classname
597 * @param string menu name
598 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200599 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000600if ( ! function_exists('timezone_menu'))
601{
602 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
603 {
604 $CI =& get_instance();
605 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200606
Greg Akerc964e722011-08-29 19:31:29 -0500607 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
609 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 if ($class != '')
612 {
613 $menu .= ' class="'.$class.'"';
614 }
Barry Mienydd671972010-10-04 16:33:58 +0200615
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 foreach (timezones() as $key => $val)
619 {
620 $selected = ($default == $key) ? " selected='selected'" : '';
621 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
622 }
623
624 $menu .= "</select>";
625
626 return $menu;
627 }
628}
Barry Mienydd671972010-10-04 16:33:58 +0200629
Derek Allard2067d1a2008-11-13 22:59:24 +0000630// ------------------------------------------------------------------------
631
632/**
633 * Timezones
634 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500635 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000636 * for various other ones in this library
637 *
638 * @access public
639 * @param string timezone
640 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200641 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000642if ( ! function_exists('timezones'))
643{
644 function timezones($tz = '')
645 {
646 // Note: Don't change the order of these even though
647 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200648
649 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500650 'UM12' => -12,
651 'UM11' => -11,
652 'UM10' => -10,
653 'UM95' => -9.5,
654 'UM9' => -9,
655 'UM8' => -8,
656 'UM7' => -7,
657 'UM6' => -6,
658 'UM5' => -5,
659 'UM45' => -4.5,
660 'UM4' => -4,
661 'UM35' => -3.5,
662 'UM3' => -3,
663 'UM2' => -2,
664 'UM1' => -1,
665 'UTC' => 0,
666 'UP1' => +1,
667 'UP2' => +2,
668 'UP3' => +3,
669 'UP35' => +3.5,
670 'UP4' => +4,
671 'UP45' => +4.5,
672 'UP5' => +5,
673 'UP55' => +5.5,
674 'UP575' => +5.75,
675 'UP6' => +6,
676 'UP65' => +6.5,
677 'UP7' => +7,
678 'UP8' => +8,
679 'UP875' => +8.75,
680 'UP9' => +9,
681 'UP95' => +9.5,
682 'UP10' => +10,
683 'UP105' => +10.5,
684 'UP11' => +11,
685 'UP115' => +11.5,
686 'UP12' => +12,
687 'UP1275' => +12.75,
688 'UP13' => +13,
689 'UP14' => +14
690 );
Barry Mienydd671972010-10-04 16:33:58 +0200691
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 if ($tz == '')
693 {
694 return $zones;
695 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500696
Greg Akerf9168392011-08-29 19:29:05 -0500697 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
700 }
701}
702
Derek Allard2067d1a2008-11-13 22:59:24 +0000703/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000704/* Location: ./system/helpers/date_helper.php */