blob: 2cbfd5a92e4dd95241338fe155b2be626b6134e9 [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
Roger Herbertb81f9092012-03-12 12:46:02 +0000162 * @param integer a number of display units
163 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000165if ( ! function_exists('timespan'))
166{
Roger Herbert8d69aa12012-03-14 08:44:55 +0000167 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
169 $CI =& get_instance();
170 $CI->lang->load('date');
171
172 if ( ! is_numeric($seconds))
173 {
174 $seconds = 1;
175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if ( ! is_numeric($time))
178 {
179 $time = time();
180 }
Barry Mienydd671972010-10-04 16:33:58 +0200181
Roger Herbert04c146d2012-03-11 15:31:01 +0000182 if ( ! is_numeric($units))
183 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000184 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000185 }
186
Roger Herbert11f46f72012-03-11 15:11:44 +0000187 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200188
Roger Herbert04c146d2012-03-11 15:31:01 +0000189 $str = array();
Roger Herbert11f46f72012-03-11 15:11:44 +0000190 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200193 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000194 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200195 }
196
Roger Herbert11f46f72012-03-11 15:11:44 +0000197 $seconds -= $years * 31557600;
198 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200199
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000200 if (count($str) < $units && ($years > 0 OR $months > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000201 {
202 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200203 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000204 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000205 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000206
207 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 }
209
Roger Herbert11f46f72012-03-11 15:11:44 +0000210 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200211
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000212 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000213 {
214 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200215 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000216 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000217 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000218
219 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200220 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000221
Roger Herbert11f46f72012-03-11 15:11:44 +0000222 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000224 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000225 {
226 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000228 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000229 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000230
231 $seconds -= $days * 86400;
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Roger Herbert11f46f72012-03-11 15:11:44 +0000234 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200235
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000236 if (count($str) < $units && ($days > 0 OR $hours > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000237 {
238 if ($hours > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000240 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Roger Herbert03dbcf02012-03-11 11:48:50 +0000241 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000242
243 $seconds -= $hours * 3600;
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Roger Herbert11f46f72012-03-11 15:11:44 +0000246 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200247
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000248 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Roger Herbert11f46f72012-03-11 15:11:44 +0000249 {
250 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200251 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000252 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Roger Herbert11f46f72012-03-11 15:11:44 +0000254
255 $seconds -= $minutes * 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Roger Herbert597eb212012-03-14 09:06:17 +0000258 if (count($str) === 0)
Roger Herbert11f46f72012-03-11 15:11:44 +0000259 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000260 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 }
Barry Mienydd671972010-10-04 16:33:58 +0200262
Roger Herbert04c146d2012-03-11 15:31:01 +0000263 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265}
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267// ------------------------------------------------------------------------
268
269/**
270 * Number of days in a month
271 *
272 * Takes a month/year as input and returns the number of days
273 * for the given month/year. Takes leap years into consideration.
274 *
275 * @access public
276 * @param integer a numeric month
277 * @param integer a numeric year
278 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200279 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000280if ( ! function_exists('days_in_month'))
281{
282 function days_in_month($month = 0, $year = '')
283 {
284 if ($month < 1 OR $month > 12)
285 {
286 return 0;
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 if ( ! is_numeric($year) OR strlen($year) != 4)
290 {
291 $year = date('Y');
292 }
Barry Mienydd671972010-10-04 16:33:58 +0200293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 if ($month == 2)
295 {
296 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
297 {
298 return 29;
299 }
300 }
301
302 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
303 return $days_in_month[$month - 1];
304 }
305}
Derek Jones36591092010-03-05 10:05:51 -0600306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307// ------------------------------------------------------------------------
308
309/**
310 * Converts a local Unix timestamp to GMT
311 *
312 * @access public
313 * @param integer Unix timestamp
314 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200315 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000316if ( ! function_exists('local_to_gmt'))
317{
318 function local_to_gmt($time = '')
319 {
320 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500321 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500323 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500324
Greg Akerf9168392011-08-29 19:29:05 -0500325 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500326 gmdate("H", $time),
327 gmdate("i", $time),
328 gmdate("s", $time),
329 gmdate("m", $time),
330 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500331 gmdate("Y", $time)
332 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
334}
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336// ------------------------------------------------------------------------
337
338/**
339 * Converts GMT time to a localized value
340 *
341 * Takes a Unix timestamp (in GMT) as input, and returns
342 * at the local value based on the timezone and DST setting
343 * submitted
344 *
345 * @access public
346 * @param integer Unix timestamp
347 * @param string timezone
348 * @param bool whether DST is active
349 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000351if ( ! function_exists('gmt_to_local'))
352{
353 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200354 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 if ($time == '')
356 {
357 return now();
358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 $time += timezones($timezone) * 3600;
361
362 if ($dst == TRUE)
363 {
364 $time += 3600;
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return $time;
368 }
369}
Derek Jones36591092010-03-05 10:05:51 -0600370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371// ------------------------------------------------------------------------
372
373/**
374 * Converts a MySQL Timestamp to Unix
375 *
376 * @access public
377 * @param integer Unix timestamp
378 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200379 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000380if ( ! function_exists('mysql_to_unix'))
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
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $time = str_replace('-', '', $time);
389 $time = str_replace(':', '', $time);
390 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500393 return mktime(
394 substr($time, 8, 2),
395 substr($time, 10, 2),
396 substr($time, 12, 2),
397 substr($time, 4, 2),
398 substr($time, 6, 2),
399 substr($time, 0, 4)
400 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 }
402}
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404// ------------------------------------------------------------------------
405
406/**
407 * Unix to "Human"
408 *
409 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
410 *
411 * @access public
412 * @param integer Unix timestamp
413 * @param bool whether to show seconds
414 * @param string format: us or euro
415 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200416 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000417if ( ! function_exists('unix_to_human'))
418{
419 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
420 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500421 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if ($fmt == 'us')
424 {
425 $r .= date('h', $time).':'.date('i', $time);
426 }
427 else
428 {
429 $r .= date('H', $time).':'.date('i', $time);
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 if ($seconds)
433 {
434 $r .= ':'.date('s', $time);
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 if ($fmt == 'us')
438 {
439 $r .= ' '.date('A', $time);
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 return $r;
443 }
444}
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446// ------------------------------------------------------------------------
447
448/**
449 * Convert "human" date to GMT
450 *
451 * Reverses the above process
452 *
453 * @access public
454 * @param string format: us or euro
455 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200456 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000457if ( ! function_exists('human_to_unix'))
458{
459 function human_to_unix($datestr = '')
460 {
461 if ($datestr == '')
462 {
463 return FALSE;
464 }
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600467 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 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))
470 {
471 return FALSE;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Jones36591092010-03-05 10:05:51 -0600474 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000475
476 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Jones4b9c6292011-07-01 17:40:48 -0500478 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
479 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
480 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000481
482 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500485 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000486
Derek Jones1322ec52009-03-11 17:01:14 +0000487 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500489 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491 else
492 {
493 // Unless specified, seconds get set to zero.
494 $sec = '00';
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if (isset($split['2']))
498 {
499 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500502 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500503 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500504 }
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500507 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500508 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500509 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500512 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500513 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500514 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 return mktime($hour, $min, $sec, $month, $day, $year);
518 }
519}
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521// ------------------------------------------------------------------------
522
523/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300524 * Turns many "reasonably-date-like" strings into something
525 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500526 *
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 * @access public
528 * @param string The terribly formatted date-like string
529 * @param string Date format to return (same as php date function)
530 * @return string
531 */
532if ( ! function_exists('nice_date'))
533{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500534 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300535 {
536 if (empty($bad_date))
537 {
538 return 'Unknown';
539 }
Greg Akerf9168392011-08-29 19:29:05 -0500540
Kyle Farris896d95a2011-08-21 23:03:54 -0300541 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500542 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300543 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500544 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300545 {
546 $year = substr($bad_date, 0, 4);
547 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548 }
549 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 {
551 $month = substr($bad_date, 0, 2);
552 $year = substr($bad_date, 2, 4);
553 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500554
Kyle Farris896d95a2011-08-21 23:03:54 -0300555 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300556 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500557
Kyle Farris896d95a2011-08-21 23:03:54 -0300558 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500559 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300560 {
561 $month = substr($bad_date, 0, 2);
562 $day = substr($bad_date, 2, 2);
563 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500564
Kyle Farris896d95a2011-08-21 23:03:54 -0300565 return date($format, strtotime($month . '/01/' . $year));
566 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500567
Kyle Farris896d95a2011-08-21 23:03:54 -0300568 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
569 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500570 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300571 list($m, $d, $y) = explode('-', $bad_date);
572 return date($format, strtotime("{$y}-{$m}-{$d}"));
573 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500574
Kyle Farris896d95a2011-08-21 23:03:54 -0300575 // Any other kind of string, when converted into UNIX time,
576 // produces "0 seconds after epoc..." is probably bad...
577 // return "Invalid Date".
578 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500579 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300580 return "Invalid Date";
581 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500582
Kyle Farris896d95a2011-08-21 23:03:54 -0300583 // It's probably a valid-ish date format already
584 return date($format, strtotime($bad_date));
585 }
586}
587
588// ------------------------------------------------------------------------
589
590/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 * Timezone Menu
592 *
593 * Generates a drop-down menu of timezones.
594 *
595 * @access public
596 * @param string timezone
597 * @param string classname
598 * @param string menu name
599 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200600 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000601if ( ! function_exists('timezone_menu'))
602{
603 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
604 {
605 $CI =& get_instance();
606 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200607
Greg Akerc964e722011-08-29 19:31:29 -0500608 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000609
610 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 if ($class != '')
613 {
614 $menu .= ' class="'.$class.'"';
615 }
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 foreach (timezones() as $key => $val)
620 {
621 $selected = ($default == $key) ? " selected='selected'" : '';
622 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
623 }
624
625 $menu .= "</select>";
626
627 return $menu;
628 }
629}
Barry Mienydd671972010-10-04 16:33:58 +0200630
Derek Allard2067d1a2008-11-13 22:59:24 +0000631// ------------------------------------------------------------------------
632
633/**
634 * Timezones
635 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500636 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 * for various other ones in this library
638 *
639 * @access public
640 * @param string timezone
641 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200642 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000643if ( ! function_exists('timezones'))
644{
645 function timezones($tz = '')
646 {
647 // Note: Don't change the order of these even though
648 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200649
650 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500651 'UM12' => -12,
652 'UM11' => -11,
653 'UM10' => -10,
654 'UM95' => -9.5,
655 'UM9' => -9,
656 'UM8' => -8,
657 'UM7' => -7,
658 'UM6' => -6,
659 'UM5' => -5,
660 'UM45' => -4.5,
661 'UM4' => -4,
662 'UM35' => -3.5,
663 'UM3' => -3,
664 'UM2' => -2,
665 'UM1' => -1,
666 'UTC' => 0,
667 'UP1' => +1,
668 'UP2' => +2,
669 'UP3' => +3,
670 'UP35' => +3.5,
671 'UP4' => +4,
672 'UP45' => +4.5,
673 'UP5' => +5,
674 'UP55' => +5.5,
675 'UP575' => +5.75,
676 'UP6' => +6,
677 'UP65' => +6.5,
678 'UP7' => +7,
679 'UP8' => +8,
680 'UP875' => +8.75,
681 'UP9' => +9,
682 'UP95' => +9.5,
683 'UP10' => +10,
684 'UP105' => +10.5,
685 'UP11' => +11,
686 'UP115' => +11.5,
687 'UP12' => +12,
688 'UP1275' => +12.75,
689 'UP13' => +13,
690 'UP14' => +14
691 );
Barry Mienydd671972010-10-04 16:33:58 +0200692
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 if ($tz == '')
694 {
695 return $zones;
696 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500697
Greg Akerf9168392011-08-29 19:29:05 -0500698 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
701 }
702}
703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000705/* Location: ./system/helpers/date_helper.php */