blob: 9f8e05bb9c89a89f43ab1f9d4e66e0c84f582a44 [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 Herbert03dbcf02012-03-11 11:48:50 +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 Herbert03dbcf02012-03-11 11:48:50 +0000181 if ( ! is_numeric($units))
182 {
183 $units = 999;
184 }
Barry Mienydd671972010-10-04 16:33:58 +0200185
Roger Herbert03dbcf02012-03-11 11:48:50 +0000186 if ($time <= $seconds)
187 {
188 $seconds = 1;
189 }
190 else
191 {
192 $seconds = $time - $seconds;
193 }
194
195 $str = array();
196
197 // years
198
199 $years = floor($seconds / 31536000);
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200202 {
Roger Herbert03dbcf02012-03-11 11:48:50 +0000203 $str[] = $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year'));
Barry Mienydd671972010-10-04 16:33:58 +0200204 }
205
Roger Herbert03dbcf02012-03-11 11:48:50 +0000206 $seconds -= $years * 31536000;
Barry Mienydd671972010-10-04 16:33:58 +0200207
Roger Herbert03dbcf02012-03-11 11:48:50 +0000208 // months
209
210 if (count($str) < $units) {
211 $months = floor($seconds / 2628000);
212
213 if ($years > 0 OR $months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200214 {
Roger Herbert03dbcf02012-03-11 11:48:50 +0000215 if ($months > 0)
216 {
217 $str[] = $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month'));
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Roger Herbert03dbcf02012-03-11 11:48:50 +0000220 $seconds -= $months * 2628000;
221 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
Roger Herbert03dbcf02012-03-11 11:48:50 +0000224 // weeks
225
226 if (count($str) < $units) {
227 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200228
Roger Herbert03dbcf02012-03-11 11:48:50 +0000229 if ($years > 0 OR $months > 0 OR $weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200230 {
Roger Herbert03dbcf02012-03-11 11:48:50 +0000231 if ($weeks > 0)
232 {
233 $str[] = $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week'));
234 }
Barry Mienydd671972010-10-04 16:33:58 +0200235
Roger Herbert03dbcf02012-03-11 11:48:50 +0000236 $seconds -= $weeks * 604800;
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000239
Roger Herbert03dbcf02012-03-11 11:48:50 +0000240 // days
Barry Mienydd671972010-10-04 16:33:58 +0200241
Roger Herbert03dbcf02012-03-11 11:48:50 +0000242 if (count($str) < $units) {
243 $days = floor($seconds / 86400);
244
245 if ($months > 0 OR $weeks > 0 OR $days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200246 {
Roger Herbert03dbcf02012-03-11 11:48:50 +0000247 if ($days > 0)
248 {
249 $str[] = $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day'));
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Roger Herbert03dbcf02012-03-11 11:48:50 +0000252 $seconds -= $days * 86400;
253 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Roger Herbert03dbcf02012-03-11 11:48:50 +0000256 // hours
Barry Mienydd671972010-10-04 16:33:58 +0200257
Roger Herbert03dbcf02012-03-11 11:48:50 +0000258 if (count($str) < $units) {
259
260 $hours = floor($seconds / 3600);
261
262 if ($days > 0 OR $hours > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
Roger Herbert03dbcf02012-03-11 11:48:50 +0000264 if ($hours > 0)
265 {
266 $str[] = $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour'));
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Roger Herbert03dbcf02012-03-11 11:48:50 +0000269 $seconds -= $hours * 3600;
270 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Roger Herbert03dbcf02012-03-11 11:48:50 +0000273 // minutes
Barry Mienydd671972010-10-04 16:33:58 +0200274
Roger Herbert03dbcf02012-03-11 11:48:50 +0000275 if (count($str) < $units) {
276
277 $minutes = floor($seconds / 60);
278
279 if ($days > 0 OR $hours > 0 OR $minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200280 {
Roger Herbert03dbcf02012-03-11 11:48:50 +0000281 if ($minutes > 0)
282 {
283 $str[] = $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute'));
284 }
285
286 $seconds -= $minutes * 60;
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Roger Herbert03dbcf02012-03-11 11:48:50 +0000290 // seconds
291
292 if (count($str) == 0) {
293 {
294 $str[] = $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second'));
295 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Roger Herbert03dbcf02012-03-11 11:48:50 +0000298 return strtolower(implode(', ', $str));
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300}
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302// ------------------------------------------------------------------------
303
304/**
305 * Number of days in a month
306 *
307 * Takes a month/year as input and returns the number of days
308 * for the given month/year. Takes leap years into consideration.
309 *
310 * @access public
311 * @param integer a numeric month
312 * @param integer a numeric year
313 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000315if ( ! function_exists('days_in_month'))
316{
317 function days_in_month($month = 0, $year = '')
318 {
319 if ($month < 1 OR $month > 12)
320 {
321 return 0;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 if ( ! is_numeric($year) OR strlen($year) != 4)
325 {
326 $year = date('Y');
327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 if ($month == 2)
330 {
331 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
332 {
333 return 29;
334 }
335 }
336
337 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
338 return $days_in_month[$month - 1];
339 }
340}
Derek Jones36591092010-03-05 10:05:51 -0600341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342// ------------------------------------------------------------------------
343
344/**
345 * Converts a local Unix timestamp to GMT
346 *
347 * @access public
348 * @param integer Unix timestamp
349 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200350 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000351if ( ! function_exists('local_to_gmt'))
352{
353 function local_to_gmt($time = '')
354 {
355 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500356 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500358 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500359
Greg Akerf9168392011-08-29 19:29:05 -0500360 return mktime(
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500361 gmdate("H", $time),
362 gmdate("i", $time),
363 gmdate("s", $time),
364 gmdate("m", $time),
365 gmdate("d", $time),
Greg Akerf9168392011-08-29 19:29:05 -0500366 gmdate("Y", $time)
367 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369}
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371// ------------------------------------------------------------------------
372
373/**
374 * Converts GMT time to a localized value
375 *
376 * Takes a Unix timestamp (in GMT) as input, and returns
377 * at the local value based on the timezone and DST setting
378 * submitted
379 *
380 * @access public
381 * @param integer Unix timestamp
382 * @param string timezone
383 * @param bool whether DST is active
384 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200385 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000386if ( ! function_exists('gmt_to_local'))
387{
388 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200389 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 if ($time == '')
391 {
392 return now();
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 $time += timezones($timezone) * 3600;
396
397 if ($dst == TRUE)
398 {
399 $time += 3600;
400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 return $time;
403 }
404}
Derek Jones36591092010-03-05 10:05:51 -0600405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406// ------------------------------------------------------------------------
407
408/**
409 * Converts a MySQL Timestamp to Unix
410 *
411 * @access public
412 * @param integer Unix timestamp
413 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200414 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000415if ( ! function_exists('mysql_to_unix'))
416{
417 function mysql_to_unix($time = '')
418 {
419 // We'll remove certain characters for backward compatibility
420 // since the formatting changed with MySQL 4.1
421 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 $time = str_replace('-', '', $time);
424 $time = str_replace(':', '', $time);
425 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500428 return mktime(
429 substr($time, 8, 2),
430 substr($time, 10, 2),
431 substr($time, 12, 2),
432 substr($time, 4, 2),
433 substr($time, 6, 2),
434 substr($time, 0, 4)
435 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
437}
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439// ------------------------------------------------------------------------
440
441/**
442 * Unix to "Human"
443 *
444 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
445 *
446 * @access public
447 * @param integer Unix timestamp
448 * @param bool whether to show seconds
449 * @param string format: us or euro
450 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200451 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000452if ( ! function_exists('unix_to_human'))
453{
454 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
455 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500456 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 if ($fmt == 'us')
459 {
460 $r .= date('h', $time).':'.date('i', $time);
461 }
462 else
463 {
464 $r .= date('H', $time).':'.date('i', $time);
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 if ($seconds)
468 {
469 $r .= ':'.date('s', $time);
470 }
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 if ($fmt == 'us')
473 {
474 $r .= ' '.date('A', $time);
475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 return $r;
478 }
479}
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481// ------------------------------------------------------------------------
482
483/**
484 * Convert "human" date to GMT
485 *
486 * Reverses the above process
487 *
488 * @access public
489 * @param string format: us or euro
490 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000492if ( ! function_exists('human_to_unix'))
493{
494 function human_to_unix($datestr = '')
495 {
496 if ($datestr == '')
497 {
498 return FALSE;
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600502 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 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))
505 {
506 return FALSE;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Jones36591092010-03-05 10:05:51 -0600509 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000510
511 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Jones4b9c6292011-07-01 17:40:48 -0500513 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
514 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
515 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000516
517 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500520 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000521
Derek Jones1322ec52009-03-11 17:01:14 +0000522 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500524 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 }
526 else
527 {
528 // Unless specified, seconds get set to zero.
529 $sec = '00';
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 if (isset($split['2']))
533 {
534 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500537 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500538 $hour = $hour + 12;
Greg Akerf9168392011-08-29 19:29:05 -0500539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500542 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500543 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500544 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500547 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500548 $hour = '0'.$hour;
Greg Akerf9168392011-08-29 19:29:05 -0500549 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 }
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 return mktime($hour, $min, $sec, $month, $day, $year);
553 }
554}
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556// ------------------------------------------------------------------------
557
558/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300559 * Turns many "reasonably-date-like" strings into something
560 * that is actually useful. This only works for dates after unix epoch.
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500561 *
Kyle Farris896d95a2011-08-21 23:03:54 -0300562 * @access public
563 * @param string The terribly formatted date-like string
564 * @param string Date format to return (same as php date function)
565 * @return string
566 */
567if ( ! function_exists('nice_date'))
568{
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500569 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300570 {
571 if (empty($bad_date))
572 {
573 return 'Unknown';
574 }
Greg Akerf9168392011-08-29 19:29:05 -0500575
Kyle Farris896d95a2011-08-21 23:03:54 -0300576 // Date like: YYYYMM
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500577 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300578 {
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500579 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
Kyle Farris896d95a2011-08-21 23:03:54 -0300580 {
581 $year = substr($bad_date, 0, 4);
582 $month = substr($bad_date, 4, 2);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500583 }
584 else
Kyle Farris896d95a2011-08-21 23:03:54 -0300585 {
586 $month = substr($bad_date, 0, 2);
587 $year = substr($bad_date, 2, 4);
588 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500589
Kyle Farris896d95a2011-08-21 23:03:54 -0300590 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300591 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500592
Kyle Farris896d95a2011-08-21 23:03:54 -0300593 // Date Like: YYYYMMDD
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500594 if (preg_match('/^\d{8}$/',$bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300595 {
596 $month = substr($bad_date, 0, 2);
597 $day = substr($bad_date, 2, 2);
598 $year = substr($bad_date, 4, 4);
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500599
Kyle Farris896d95a2011-08-21 23:03:54 -0300600 return date($format, strtotime($month . '/01/' . $year));
601 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500602
Kyle Farris896d95a2011-08-21 23:03:54 -0300603 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
604 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500605 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300606 list($m, $d, $y) = explode('-', $bad_date);
607 return date($format, strtotime("{$y}-{$m}-{$d}"));
608 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500609
Kyle Farris896d95a2011-08-21 23:03:54 -0300610 // Any other kind of string, when converted into UNIX time,
611 // produces "0 seconds after epoc..." is probably bad...
612 // return "Invalid Date".
613 if (date('U', strtotime($bad_date)) == '0')
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500614 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300615 return "Invalid Date";
616 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500617
Kyle Farris896d95a2011-08-21 23:03:54 -0300618 // It's probably a valid-ish date format already
619 return date($format, strtotime($bad_date));
620 }
621}
622
623// ------------------------------------------------------------------------
624
625/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000626 * Timezone Menu
627 *
628 * Generates a drop-down menu of timezones.
629 *
630 * @access public
631 * @param string timezone
632 * @param string classname
633 * @param string menu name
634 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200635 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000636if ( ! function_exists('timezone_menu'))
637{
638 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
639 {
640 $CI =& get_instance();
641 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200642
Greg Akerc964e722011-08-29 19:31:29 -0500643 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000644
645 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200646
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 if ($class != '')
648 {
649 $menu .= ' class="'.$class.'"';
650 }
Barry Mienydd671972010-10-04 16:33:58 +0200651
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 foreach (timezones() as $key => $val)
655 {
656 $selected = ($default == $key) ? " selected='selected'" : '';
657 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
658 }
659
660 $menu .= "</select>";
661
662 return $menu;
663 }
664}
Barry Mienydd671972010-10-04 16:33:58 +0200665
Derek Allard2067d1a2008-11-13 22:59:24 +0000666// ------------------------------------------------------------------------
667
668/**
669 * Timezones
670 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500671 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 * for various other ones in this library
673 *
674 * @access public
675 * @param string timezone
676 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200677 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000678if ( ! function_exists('timezones'))
679{
680 function timezones($tz = '')
681 {
682 // Note: Don't change the order of these even though
683 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200684
685 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500686 'UM12' => -12,
687 'UM11' => -11,
688 'UM10' => -10,
689 'UM95' => -9.5,
690 'UM9' => -9,
691 'UM8' => -8,
692 'UM7' => -7,
693 'UM6' => -6,
694 'UM5' => -5,
695 'UM45' => -4.5,
696 'UM4' => -4,
697 'UM35' => -3.5,
698 'UM3' => -3,
699 'UM2' => -2,
700 'UM1' => -1,
701 'UTC' => 0,
702 'UP1' => +1,
703 'UP2' => +2,
704 'UP3' => +3,
705 'UP35' => +3.5,
706 'UP4' => +4,
707 'UP45' => +4.5,
708 'UP5' => +5,
709 'UP55' => +5.5,
710 'UP575' => +5.75,
711 'UP6' => +6,
712 'UP65' => +6.5,
713 'UP7' => +7,
714 'UP8' => +8,
715 'UP875' => +8.75,
716 'UP9' => +9,
717 'UP95' => +9.5,
718 'UP10' => +10,
719 'UP105' => +10.5,
720 'UP11' => +11,
721 'UP115' => +11.5,
722 'UP12' => +12,
723 'UP1275' => +12.75,
724 'UP13' => +13,
725 'UP14' => +14
726 );
Barry Mienydd671972010-10-04 16:33:58 +0200727
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 if ($tz == '')
729 {
730 return $zones;
731 }
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500732
Greg Akerf9168392011-08-29 19:29:05 -0500733 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
Eric Barnesdc3e4be2011-12-19 13:48:29 -0500734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
736 }
737}
738
Derek Allard2067d1a2008-11-13 22:59:24 +0000739/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000740/* Location: ./system/helpers/date_helper.php */