blob: d54553292c6a23d13a4d2616ae24da2092cc6765 [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
Roger Herbertb81f9092012-03-12 12:46:02 +0000163 * @param integer a number of display units
164 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200165 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000166if ( ! function_exists('timespan'))
167{
Roger Herbert8d69aa12012-03-14 08:44:55 +0000168 function timespan($seconds = 1, $time = '', $units = 7)
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
170 $CI =& get_instance();
171 $CI->lang->load('date');
172
173 if ( ! is_numeric($seconds))
174 {
175 $seconds = 1;
176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if ( ! is_numeric($time))
179 {
180 $time = time();
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Roger Herbert04c146d2012-03-11 15:31:01 +0000183 if ( ! is_numeric($units))
184 {
Roger Herbert8d69aa12012-03-14 08:44:55 +0000185 $units = 7;
Roger Herbert04c146d2012-03-11 15:31:01 +0000186 }
187
Greg Akerf9168392011-08-29 19:29:05 -0500188 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200189
Roger Herbert04c146d2012-03-11 15:31:01 +0000190 $str = array();
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500191 $years = floor($seconds / 31557600);
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200194 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000195 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200196 }
197
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500198 $seconds -= $years * 31557600;
199 $months = floor($seconds / 2629743);
Barry Mienydd671972010-10-04 16:33:58 +0200200
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000201 if (count($str) < $units && ($years > 0 OR $months > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
203 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200204 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000205 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
207
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500208 $seconds -= $months * 2629743;
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 }
210
211 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200212
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000213 if (count($str) < $units && ($years > 0 OR $months > 0 OR $weeks > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 {
215 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000217 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200221 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000222
223 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000225 if (count($str) < $units && ($months > 0 OR $weeks > 0 OR $days > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200228 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000229 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $seconds -= $days * 86400;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200236
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000237 if (count($str) < $units && ($days > 0 OR $hours > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
239 if ($hours > 0)
240 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000241 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 $seconds -= $hours * 3600;
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200248
Roger Herbertb8fb66b2012-03-11 16:15:15 +0000249 if (count($str) < $units && ($days > 0 OR $hours > 0 OR $minutes > 0))
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
251 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200252 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000253 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 $seconds -= $minutes * 60;
257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Roger Herbert597eb212012-03-14 09:06:17 +0000259 if (count($str) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Roger Herbert04c146d2012-03-11 15:31:01 +0000261 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Roger Herbert04c146d2012-03-11 15:31:01 +0000264 return implode(', ', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
266}
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268// ------------------------------------------------------------------------
269
270/**
271 * Number of days in a month
272 *
273 * Takes a month/year as input and returns the number of days
274 * for the given month/year. Takes leap years into consideration.
275 *
276 * @access public
277 * @param integer a numeric month
278 * @param integer a numeric year
279 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200280 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000281if ( ! function_exists('days_in_month'))
282{
283 function days_in_month($month = 0, $year = '')
284 {
285 if ($month < 1 OR $month > 12)
286 {
287 return 0;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 if ( ! is_numeric($year) OR strlen($year) != 4)
291 {
292 $year = date('Y');
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 if ($month == 2)
296 {
297 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
298 {
299 return 29;
300 }
301 }
302
303 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
304 return $days_in_month[$month - 1];
305 }
306}
Derek Jones36591092010-03-05 10:05:51 -0600307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308// ------------------------------------------------------------------------
309
310/**
311 * Converts a local Unix timestamp to GMT
312 *
313 * @access public
314 * @param integer Unix timestamp
315 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200316 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000317if ( ! function_exists('local_to_gmt'))
318{
319 function local_to_gmt($time = '')
320 {
321 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500322 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500324 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500325
Greg Akerf9168392011-08-29 19:29:05 -0500326 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500327 gmdate("H", $time),
328 gmdate("i", $time),
329 gmdate("s", $time),
330 gmdate("m", $time),
331 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500332 gmdate("Y", $time)
333 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
335}
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337// ------------------------------------------------------------------------
338
339/**
340 * Converts GMT time to a localized value
341 *
342 * Takes a Unix timestamp (in GMT) as input, and returns
343 * at the local value based on the timezone and DST setting
344 * submitted
345 *
346 * @access public
347 * @param integer Unix timestamp
348 * @param string timezone
349 * @param bool whether DST is active
350 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200351 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000352if ( ! function_exists('gmt_to_local'))
353{
354 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200355 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 if ($time == '')
357 {
358 return now();
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 $time += timezones($timezone) * 3600;
362
363 if ($dst == TRUE)
364 {
365 $time += 3600;
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 return $time;
369 }
370}
Derek Jones36591092010-03-05 10:05:51 -0600371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372// ------------------------------------------------------------------------
373
374/**
375 * Converts a MySQL Timestamp to Unix
376 *
377 * @access public
378 * @param integer Unix timestamp
379 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200380 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000381if ( ! function_exists('mysql_to_unix'))
382{
383 function mysql_to_unix($time = '')
384 {
385 // We'll remove certain characters for backward compatibility
386 // since the formatting changed with MySQL 4.1
387 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 $time = str_replace('-', '', $time);
390 $time = str_replace(':', '', $time);
391 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500394 return mktime(
395 substr($time, 8, 2),
396 substr($time, 10, 2),
397 substr($time, 12, 2),
398 substr($time, 4, 2),
399 substr($time, 6, 2),
400 substr($time, 0, 4)
401 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
403}
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405// ------------------------------------------------------------------------
406
407/**
408 * Unix to "Human"
409 *
410 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
411 *
412 * @access public
413 * @param integer Unix timestamp
414 * @param bool whether to show seconds
415 * @param string format: us or euro
416 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200417 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000418if ( ! function_exists('unix_to_human'))
419{
420 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
421 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500422 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 if ($fmt == 'us')
425 {
426 $r .= date('h', $time).':'.date('i', $time);
427 }
428 else
429 {
430 $r .= date('H', $time).':'.date('i', $time);
431 }
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 if ($seconds)
434 {
435 $r .= ':'.date('s', $time);
436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 if ($fmt == 'us')
439 {
440 $r .= ' '.date('A', $time);
441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 return $r;
444 }
445}
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447// ------------------------------------------------------------------------
448
449/**
450 * Convert "human" date to GMT
451 *
452 * Reverses the above process
453 *
454 * @access public
455 * @param string format: us or euro
456 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200457 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000458if ( ! function_exists('human_to_unix'))
459{
460 function human_to_unix($datestr = '')
461 {
462 if ($datestr == '')
463 {
464 return FALSE;
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600468 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 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))
471 {
472 return FALSE;
473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Jones36591092010-03-05 10:05:51 -0600475 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000476
477 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Jones4b9c6292011-07-01 17:40:48 -0500479 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
480 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
481 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000482
483 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500486 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000487
Derek Jones1322ec52009-03-11 17:01:14 +0000488 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500490 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492 else
493 {
494 // Unless specified, seconds get set to zero.
495 $sec = '00';
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 if (isset($split['2']))
499 {
500 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500503 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500504 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500505 }
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500508 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500509 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500510 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500513 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500514 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500515 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 return mktime($hour, $min, $sec, $month, $day, $year);
519 }
520}
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522// ------------------------------------------------------------------------
523
524/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 * Turns many "reasonably-date-like" strings into something
526 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500527 *
Kyle Farris896d95a2011-08-21 23:03:54 -0300528 * @access public
529 * @param string The terribly formatted date-like string
530 * @param string Date format to return (same as php date function)
531 * @return string
532 */
533if ( ! function_exists('nice_date'))
534{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500535 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300536 {
537 if (empty($bad_date))
538 {
539 return 'Unknown';
540 }
Greg Akerf9168392011-08-29 19:29:05 -0500541
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500543 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300544 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500545 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300546 {
547 $year = substr($bad_date, 0, 4);
548 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500549 }
550 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300551 {
552 $month = substr($bad_date, 0, 2);
553 $year = substr($bad_date, 2, 4);
554 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500555
Kyle Farris896d95a2011-08-21 23:03:54 -0300556 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300557 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500558
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500560 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300561 {
562 $month = substr($bad_date, 0, 2);
563 $day = substr($bad_date, 2, 2);
564 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500565
Kyle Farris896d95a2011-08-21 23:03:54 -0300566 return date($format, strtotime($month . '/01/' . $year));
567 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500568
Kyle Farris896d95a2011-08-21 23:03:54 -0300569 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
570 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500571 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300572 list($m, $d, $y) = explode('-', $bad_date);
573 return date($format, strtotime("{$y}-{$m}-{$d}"));
574 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500575
Kyle Farris896d95a2011-08-21 23:03:54 -0300576 // Any other kind of string, when converted into UNIX time,
577 // produces "0 seconds after epoc..." is probably bad...
578 // return "Invalid Date".
579 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500580 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300581 return "Invalid Date";
582 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500583
Kyle Farris896d95a2011-08-21 23:03:54 -0300584 // It's probably a valid-ish date format already
585 return date($format, strtotime($bad_date));
586 }
587}
588
589// ------------------------------------------------------------------------
590
591/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 * Timezone Menu
593 *
594 * Generates a drop-down menu of timezones.
595 *
596 * @access public
597 * @param string timezone
598 * @param string classname
599 * @param string menu name
600 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200601 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000602if ( ! function_exists('timezone_menu'))
603{
604 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
605 {
606 $CI =& get_instance();
607 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200608
Greg Akerc964e722011-08-29 19:31:29 -0500609 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000610
611 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 if ($class != '')
614 {
615 $menu .= ' class="'.$class.'"';
616 }
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 foreach (timezones() as $key => $val)
621 {
622 $selected = ($default == $key) ? " selected='selected'" : '';
623 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
624 }
625
626 $menu .= "</select>";
627
628 return $menu;
629 }
630}
Barry Mienydd671972010-10-04 16:33:58 +0200631
Derek Allard2067d1a2008-11-13 22:59:24 +0000632// ------------------------------------------------------------------------
633
634/**
635 * Timezones
636 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500637 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 * for various other ones in this library
639 *
640 * @access public
641 * @param string timezone
642 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200643 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000644if ( ! function_exists('timezones'))
645{
646 function timezones($tz = '')
647 {
648 // Note: Don't change the order of these even though
649 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200650
651 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500652 'UM12' => -12,
653 'UM11' => -11,
654 'UM10' => -10,
655 'UM95' => -9.5,
656 'UM9' => -9,
657 'UM8' => -8,
658 'UM7' => -7,
659 'UM6' => -6,
660 'UM5' => -5,
661 'UM45' => -4.5,
662 'UM4' => -4,
663 'UM35' => -3.5,
664 'UM3' => -3,
665 'UM2' => -2,
666 'UM1' => -1,
667 'UTC' => 0,
668 'UP1' => +1,
669 'UP2' => +2,
670 'UP3' => +3,
671 'UP35' => +3.5,
672 'UP4' => +4,
673 'UP45' => +4.5,
674 'UP5' => +5,
675 'UP55' => +5.5,
676 'UP575' => +5.75,
677 'UP6' => +6,
678 'UP65' => +6.5,
679 'UP7' => +7,
680 'UP8' => +8,
681 'UP875' => +8.75,
682 'UP9' => +9,
683 'UP95' => +9.5,
684 'UP10' => +10,
685 'UP105' => +10.5,
686 'UP11' => +11,
687 'UP115' => +11.5,
688 'UP12' => +12,
689 'UP1275' => +12.75,
690 'UP13' => +13,
691 'UP14' => +14
692 );
Barry Mienydd671972010-10-04 16:33:58 +0200693
Derek Allard2067d1a2008-11-13 22:59:24 +0000694 if ($tz == '')
695 {
696 return $zones;
697 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500698
Greg Akerf9168392011-08-29 19:29:05 -0500699 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
702 }
703}
704
Derek Allard2067d1a2008-11-13 22:59:24 +0000705/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000706/* Location: ./system/helpers/date_helper.php */