blob: 7e60cccf81e864f03da52c7ac8396ec7b4fbfb37 [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(
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400131 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400133 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
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',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400138 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
Eric Barnes5d1e32b2011-05-03 22:28:59 -0400140 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%O'
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 );
142
143 if ( ! isset($formats[$fmt]))
144 {
145 return FALSE;
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 return mdate($formats[$fmt], $time);
149 }
150}
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152// ------------------------------------------------------------------------
153
154/**
155 * Timespan
156 *
157 * Returns a span of seconds in this format:
158 * 10 days 14 hours 36 minutes 47 seconds
159 *
160 * @access public
161 * @param integer a number of seconds
162 * @param integer Unix timestamp
163 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000165if ( ! function_exists('timespan'))
166{
167 function timespan($seconds = 1, $time = '')
168 {
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
Greg Akerf9168392011-08-29 19:29:05 -0500182 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 $str = '';
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500185 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200188 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200190 }
191
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500192 $seconds -= $years * 31557600;
193 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 if ($years > 0 OR $months > 0)
196 {
197 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200198 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200200 }
201
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500202 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
204
205 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 if ($years > 0 OR $months > 0 OR $weeks > 0)
208 {
209 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200210 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200215 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
217 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 if ($months > 0 OR $weeks > 0 OR $days > 0)
220 {
221 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200222 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $seconds -= $days * 86400;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 if ($days > 0 OR $hours > 0)
232 {
233 if ($hours > 0)
234 {
235 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
236 }
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 $seconds -= $hours * 3600;
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 if ($days > 0 OR $hours > 0 OR $minutes > 0)
244 {
245 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200246 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
248 }
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $seconds -= $minutes * 60;
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 if ($str == '')
254 {
255 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 return substr(trim($str), 0, -1);
259 }
260}
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262// ------------------------------------------------------------------------
263
264/**
265 * Number of days in a month
266 *
267 * Takes a month/year as input and returns the number of days
268 * for the given month/year. Takes leap years into consideration.
269 *
270 * @access public
271 * @param integer a numeric month
272 * @param integer a numeric year
273 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200274 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000275if ( ! function_exists('days_in_month'))
276{
277 function days_in_month($month = 0, $year = '')
278 {
279 if ($month < 1 OR $month > 12)
280 {
281 return 0;
282 }
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 if ( ! is_numeric($year) OR strlen($year) != 4)
285 {
286 $year = date('Y');
287 }
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 if ($month == 2)
290 {
291 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
292 {
293 return 29;
294 }
295 }
296
297 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
298 return $days_in_month[$month - 1];
299 }
300}
Derek Jones36591092010-03-05 10:05:51 -0600301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302// ------------------------------------------------------------------------
303
304/**
305 * Converts a local Unix timestamp to GMT
306 *
307 * @access public
308 * @param integer Unix timestamp
309 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200310 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000311if ( ! function_exists('local_to_gmt'))
312{
313 function local_to_gmt($time = '')
314 {
315 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500316 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500318 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500319
Greg Akerf9168392011-08-29 19:29:05 -0500320 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500321 gmdate("H", $time),
322 gmdate("i", $time),
323 gmdate("s", $time),
324 gmdate("m", $time),
325 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500326 gmdate("Y", $time)
327 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 }
329}
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331// ------------------------------------------------------------------------
332
333/**
334 * Converts GMT time to a localized value
335 *
336 * Takes a Unix timestamp (in GMT) as input, and returns
337 * at the local value based on the timezone and DST setting
338 * submitted
339 *
340 * @access public
341 * @param integer Unix timestamp
342 * @param string timezone
343 * @param bool whether DST is active
344 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200345 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000346if ( ! function_exists('gmt_to_local'))
347{
348 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200349 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 if ($time == '')
351 {
352 return now();
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 $time += timezones($timezone) * 3600;
356
357 if ($dst == TRUE)
358 {
359 $time += 3600;
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 return $time;
363 }
364}
Derek Jones36591092010-03-05 10:05:51 -0600365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366// ------------------------------------------------------------------------
367
368/**
369 * Converts a MySQL Timestamp to Unix
370 *
371 * @access public
372 * @param integer Unix timestamp
373 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200374 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000375if ( ! function_exists('mysql_to_unix'))
376{
377 function mysql_to_unix($time = '')
378 {
379 // We'll remove certain characters for backward compatibility
380 // since the formatting changed with MySQL 4.1
381 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 $time = str_replace('-', '', $time);
384 $time = str_replace(':', '', $time);
385 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500388 return mktime(
389 substr($time, 8, 2),
390 substr($time, 10, 2),
391 substr($time, 12, 2),
392 substr($time, 4, 2),
393 substr($time, 6, 2),
394 substr($time, 0, 4)
395 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397}
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399// ------------------------------------------------------------------------
400
401/**
402 * Unix to "Human"
403 *
404 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
405 *
406 * @access public
407 * @param integer Unix timestamp
408 * @param bool whether to show seconds
409 * @param string format: us or euro
410 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200411 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000412if ( ! function_exists('unix_to_human'))
413{
414 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
415 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500416 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if ($fmt == 'us')
419 {
420 $r .= date('h', $time).':'.date('i', $time);
421 }
422 else
423 {
424 $r .= date('H', $time).':'.date('i', $time);
425 }
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 if ($seconds)
428 {
429 $r .= ':'.date('s', $time);
430 }
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 if ($fmt == 'us')
433 {
434 $r .= ' '.date('A', $time);
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 return $r;
438 }
439}
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441// ------------------------------------------------------------------------
442
443/**
444 * Convert "human" date to GMT
445 *
446 * Reverses the above process
447 *
448 * @access public
449 * @param string format: us or euro
450 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200451 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000452if ( ! function_exists('human_to_unix'))
453{
454 function human_to_unix($datestr = '')
455 {
456 if ($datestr == '')
457 {
458 return FALSE;
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600462 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 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))
465 {
466 return FALSE;
467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Jones36591092010-03-05 10:05:51 -0600469 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
471 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Jones4b9c6292011-07-01 17:40:48 -0500473 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
474 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
475 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000476
477 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500480 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000481
Derek Jones1322ec52009-03-11 17:01:14 +0000482 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500484 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
486 else
487 {
488 // Unless specified, seconds get set to zero.
489 $sec = '00';
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 if (isset($split['2']))
493 {
494 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500497 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500498 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500502 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500503 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500504 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500507 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500508 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500509 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 return mktime($hour, $min, $sec, $month, $day, $year);
513 }
514}
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516// ------------------------------------------------------------------------
517
518/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300519 * Turns many "reasonably-date-like" strings into something
520 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500521 *
Kyle Farris896d95a2011-08-21 23:03:54 -0300522 * @access public
523 * @param string The terribly formatted date-like string
524 * @param string Date format to return (same as php date function)
525 * @return string
526 */
527if ( ! function_exists('nice_date'))
528{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500529 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 {
531 if (empty($bad_date))
532 {
533 return 'Unknown';
534 }
Greg Akerf9168392011-08-29 19:29:05 -0500535
Kyle Farris896d95a2011-08-21 23:03:54 -0300536 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500537 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300538 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500539 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300540 {
541 $year = substr($bad_date, 0, 4);
542 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543 }
544 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300545 {
546 $month = substr($bad_date, 0, 2);
547 $year = substr($bad_date, 2, 4);
548 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500549
Kyle Farris896d95a2011-08-21 23:03:54 -0300550 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300551 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500552
Kyle Farris896d95a2011-08-21 23:03:54 -0300553 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500554 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300555 {
556 $month = substr($bad_date, 0, 2);
557 $day = substr($bad_date, 2, 2);
558 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500559
Kyle Farris896d95a2011-08-21 23:03:54 -0300560 return date($format, strtotime($month . '/01/' . $year));
561 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500562
Kyle Farris896d95a2011-08-21 23:03:54 -0300563 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
564 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500565 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300566 list($m, $d, $y) = explode('-', $bad_date);
567 return date($format, strtotime("{$y}-{$m}-{$d}"));
568 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500569
Kyle Farris896d95a2011-08-21 23:03:54 -0300570 // Any other kind of string, when converted into UNIX time,
571 // produces "0 seconds after epoc..." is probably bad...
572 // return "Invalid Date".
573 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500574 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300575 return "Invalid Date";
576 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500577
Kyle Farris896d95a2011-08-21 23:03:54 -0300578 // It's probably a valid-ish date format already
579 return date($format, strtotime($bad_date));
580 }
581}
582
583// ------------------------------------------------------------------------
584
585/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 * Timezone Menu
587 *
588 * Generates a drop-down menu of timezones.
589 *
590 * @access public
591 * @param string timezone
592 * @param string classname
593 * @param string menu name
594 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200595 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000596if ( ! function_exists('timezone_menu'))
597{
598 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
599 {
600 $CI =& get_instance();
601 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200602
Greg Akerc964e722011-08-29 19:31:29 -0500603 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000604
605 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 if ($class != '')
608 {
609 $menu .= ' class="'.$class.'"';
610 }
Barry Mienydd671972010-10-04 16:33:58 +0200611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 foreach (timezones() as $key => $val)
615 {
616 $selected = ($default == $key) ? " selected='selected'" : '';
617 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
618 }
619
620 $menu .= "</select>";
621
622 return $menu;
623 }
624}
Barry Mienydd671972010-10-04 16:33:58 +0200625
Derek Allard2067d1a2008-11-13 22:59:24 +0000626// ------------------------------------------------------------------------
627
628/**
629 * Timezones
630 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500631 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * for various other ones in this library
633 *
634 * @access public
635 * @param string timezone
636 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200637 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000638if ( ! function_exists('timezones'))
639{
640 function timezones($tz = '')
641 {
642 // Note: Don't change the order of these even though
643 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200644
645 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500646 'UM12' => -12,
647 'UM11' => -11,
648 'UM10' => -10,
649 'UM95' => -9.5,
650 'UM9' => -9,
651 'UM8' => -8,
652 'UM7' => -7,
653 'UM6' => -6,
654 'UM5' => -5,
655 'UM45' => -4.5,
656 'UM4' => -4,
657 'UM35' => -3.5,
658 'UM3' => -3,
659 'UM2' => -2,
660 'UM1' => -1,
661 'UTC' => 0,
662 'UP1' => +1,
663 'UP2' => +2,
664 'UP3' => +3,
665 'UP35' => +3.5,
666 'UP4' => +4,
667 'UP45' => +4.5,
668 'UP5' => +5,
669 'UP55' => +5.5,
670 'UP575' => +5.75,
671 'UP6' => +6,
672 'UP65' => +6.5,
673 'UP7' => +7,
674 'UP8' => +8,
675 'UP875' => +8.75,
676 'UP9' => +9,
677 'UP95' => +9.5,
678 'UP10' => +10,
679 'UP105' => +10.5,
680 'UP11' => +11,
681 'UP115' => +11.5,
682 'UP12' => +12,
683 'UP1275' => +12.75,
684 'UP13' => +13,
685 'UP14' => +14
686 );
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 if ($tz == '')
689 {
690 return $zones;
691 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500692
Greg Akerf9168392011-08-29 19:29:05 -0500693 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
696 }
697}
698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000700/* Location: ./system/helpers/date_helper.php */