blob: e14bc2f94021194c61bfe44ec2dd666c73db135a [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Date Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/date_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Get "now" time
32 *
33 * Returns time() or its GMT equivalent based on the config file preference
34 *
35 * @access public
36 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020037 */
Derek Allard2067d1a2008-11-13 22:59:24 +000038if ( ! function_exists('now'))
39{
40 function now()
41 {
42 $CI =& get_instance();
Barry Mienydd671972010-10-04 16:33:58 +020043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 if (strtolower($CI->config->item('time_reference')) == 'gmt')
45 {
46 $now = time();
47 $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 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 if (strlen($system_time) < 10)
50 {
51 $system_time = time();
52 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
53 }
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 return $system_time;
56 }
Greg Akerc964e722011-08-29 19:31:29 -050057
58 return time();
Derek Allard2067d1a2008-11-13 22:59:24 +000059 }
60}
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062// ------------------------------------------------------------------------
63
64/**
65 * Convert MySQL Style Datecodes
66 *
67 * This function is identical to PHPs date() function,
68 * except that it allows date codes to be formatted using
69 * the MySQL style, where each code letter is preceded
Derek Jones4b9c6292011-07-01 17:40:48 -050070 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000071 *
72 * The benefit of doing dates this way is that you don't
73 * have to worry about escaping your text letters that
74 * match the date codes.
75 *
76 * @access public
77 * @param string
78 * @param integer
79 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020080 */
Derek Allard2067d1a2008-11-13 22:59:24 +000081if ( ! function_exists('mdate'))
82{
83 function mdate($datestr = '', $time = '')
84 {
85 if ($datestr == '')
Greg Akerf9168392011-08-29 19:29:05 -050086 {
87 return '';
88 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Greg Akerc964e722011-08-29 19:31:29 -050090 $time = ($time == '') ? now() : $time;
Barry Mienydd671972010-10-04 16:33:58 +020091
Greg Akerf9168392011-08-29 19:29:05 -050092 $datestr = str_replace(
93 '%\\',
94 '',
95 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
96 );
Greg Akerc964e722011-08-29 19:31:29 -050097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 return date($datestr, $time);
99 }
100}
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102// ------------------------------------------------------------------------
103
104/**
105 * Standard Date
106 *
107 * Returns a date formatted according to the submitted standard.
108 *
109 * @access public
110 * @param string the chosen format
111 * @param integer Unix timestamp
112 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200113 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000114if ( ! function_exists('standard_date'))
115{
116 function standard_date($fmt = 'DATE_RFC822', $time = '')
117 {
118 $formats = array(
119 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
120 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Greg Aker3b889a92011-01-10 12:35:57 -0600121 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
123 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
124 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
125 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
126 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
127 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
128 );
129
130 if ( ! isset($formats[$fmt]))
131 {
132 return FALSE;
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 return mdate($formats[$fmt], $time);
136 }
137}
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139// ------------------------------------------------------------------------
140
141/**
142 * Timespan
143 *
144 * Returns a span of seconds in this format:
145 * 10 days 14 hours 36 minutes 47 seconds
146 *
147 * @access public
148 * @param integer a number of seconds
149 * @param integer Unix timestamp
150 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200151 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000152if ( ! function_exists('timespan'))
153{
154 function timespan($seconds = 1, $time = '')
155 {
156 $CI =& get_instance();
157 $CI->lang->load('date');
158
159 if ( ! is_numeric($seconds))
160 {
161 $seconds = 1;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 if ( ! is_numeric($time))
165 {
166 $time = time();
167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Greg Akerf9168392011-08-29 19:29:05 -0500169 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 $str = '';
172 $years = floor($seconds / 31536000);
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200175 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200177 }
178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 $seconds -= $years * 31536000;
180 $months = floor($seconds / 2628000);
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 if ($years > 0 OR $months > 0)
183 {
184 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200185 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200187 }
188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 $seconds -= $months * 2628000;
190 }
191
192 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if ($years > 0 OR $months > 0 OR $weeks > 0)
195 {
196 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200197 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200202 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000203
204 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 if ($months > 0 OR $weeks > 0 OR $days > 0)
207 {
208 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200209 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 $seconds -= $days * 86400;
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 if ($days > 0 OR $hours > 0)
219 {
220 if ($hours > 0)
221 {
222 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 $seconds -= $hours * 3600;
226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if ($days > 0 OR $hours > 0 OR $minutes > 0)
231 {
232 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200233 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 $seconds -= $minutes * 60;
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 if ($str == '')
241 {
242 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 return substr(trim($str), 0, -1);
246 }
247}
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249// ------------------------------------------------------------------------
250
251/**
252 * Number of days in a month
253 *
254 * Takes a month/year as input and returns the number of days
255 * for the given month/year. Takes leap years into consideration.
256 *
257 * @access public
258 * @param integer a numeric month
259 * @param integer a numeric year
260 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200261 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000262if ( ! function_exists('days_in_month'))
263{
264 function days_in_month($month = 0, $year = '')
265 {
266 if ($month < 1 OR $month > 12)
267 {
268 return 0;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 if ( ! is_numeric($year) OR strlen($year) != 4)
272 {
273 $year = date('Y');
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 if ($month == 2)
277 {
278 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
279 {
280 return 29;
281 }
282 }
283
284 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
285 return $days_in_month[$month - 1];
286 }
287}
Derek Jones36591092010-03-05 10:05:51 -0600288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289// ------------------------------------------------------------------------
290
291/**
292 * Converts a local Unix timestamp to GMT
293 *
294 * @access public
295 * @param integer Unix timestamp
296 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200297 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000298if ( ! function_exists('local_to_gmt'))
299{
300 function local_to_gmt($time = '')
301 {
302 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500303 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500305 }
306
307 return mktime(
308 gmdate("H", $time),
309 gmdate("i", $time),
310 gmdate("s", $time),
311 gmdate("m", $time),
312 gmdate("d", $time),
313 gmdate("Y", $time)
314 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316}
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318// ------------------------------------------------------------------------
319
320/**
321 * Converts GMT time to a localized value
322 *
323 * Takes a Unix timestamp (in GMT) as input, and returns
324 * at the local value based on the timezone and DST setting
325 * submitted
326 *
327 * @access public
328 * @param integer Unix timestamp
329 * @param string timezone
330 * @param bool whether DST is active
331 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200332 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000333if ( ! function_exists('gmt_to_local'))
334{
335 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200336 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 if ($time == '')
338 {
339 return now();
340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 $time += timezones($timezone) * 3600;
343
344 if ($dst == TRUE)
345 {
346 $time += 3600;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 return $time;
350 }
351}
Derek Jones36591092010-03-05 10:05:51 -0600352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353// ------------------------------------------------------------------------
354
355/**
356 * Converts a MySQL Timestamp to Unix
357 *
358 * @access public
359 * @param integer Unix timestamp
360 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200361 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000362if ( ! function_exists('mysql_to_unix'))
363{
364 function mysql_to_unix($time = '')
365 {
366 // We'll remove certain characters for backward compatibility
367 // since the formatting changed with MySQL 4.1
368 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 $time = str_replace('-', '', $time);
371 $time = str_replace(':', '', $time);
372 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // YYYYMMDDHHMMSS
Greg Akerc964e722011-08-29 19:31:29 -0500375 return mktime(
376 substr($time, 8, 2),
377 substr($time, 10, 2),
378 substr($time, 12, 2),
379 substr($time, 4, 2),
380 substr($time, 6, 2),
381 substr($time, 0, 4)
382 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384}
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386// ------------------------------------------------------------------------
387
388/**
389 * Unix to "Human"
390 *
391 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
392 *
393 * @access public
394 * @param integer Unix timestamp
395 * @param bool whether to show seconds
396 * @param string format: us or euro
397 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200398 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000399if ( ! function_exists('unix_to_human'))
400{
401 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
402 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500403 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 if ($fmt == 'us')
406 {
407 $r .= date('h', $time).':'.date('i', $time);
408 }
409 else
410 {
411 $r .= date('H', $time).':'.date('i', $time);
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 if ($seconds)
415 {
416 $r .= ':'.date('s', $time);
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 if ($fmt == 'us')
420 {
421 $r .= ' '.date('A', $time);
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 return $r;
425 }
426}
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428// ------------------------------------------------------------------------
429
430/**
431 * Convert "human" date to GMT
432 *
433 * Reverses the above process
434 *
435 * @access public
436 * @param string format: us or euro
437 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200438 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000439if ( ! function_exists('human_to_unix'))
440{
441 function human_to_unix($datestr = '')
442 {
443 if ($datestr == '')
444 {
445 return FALSE;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600449 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 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))
452 {
453 return FALSE;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Jones36591092010-03-05 10:05:51 -0600456 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000457
458 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Jones4b9c6292011-07-01 17:40:48 -0500460 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
461 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
462 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
464 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500467 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000468
Derek Jones1322ec52009-03-11 17:01:14 +0000469 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500471 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
473 else
474 {
475 // Unless specified, seconds get set to zero.
476 $sec = '00';
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 if (isset($split['2']))
480 {
481 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500484 {
485 $hour = $hour + 12;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500489 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500490 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500491 }
492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500494 {
495 $hour = '0'.$hour;
496 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 return mktime($hour, $min, $sec, $month, $day, $year);
500 }
501}
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503// ------------------------------------------------------------------------
504
505/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300506 * Turns many "reasonably-date-like" strings into something
507 * that is actually useful. This only works for dates after unix epoch.
508 *
509 * @access public
510 * @param string The terribly formatted date-like string
511 * @param string Date format to return (same as php date function)
512 * @return string
513 */
514if ( ! function_exists('nice_date'))
515{
Greg Akerf9168392011-08-29 19:29:05 -0500516 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300517 {
518 if (empty($bad_date))
519 {
520 return 'Unknown';
521 }
Greg Akerf9168392011-08-29 19:29:05 -0500522
Kyle Farris896d95a2011-08-21 23:03:54 -0300523 // Date like: YYYYMM
Greg Akerf9168392011-08-29 19:29:05 -0500524 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300525 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300526 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
527 {
528 $year = substr($bad_date, 0, 4);
529 $month = substr($bad_date, 4, 2);
530 }
531 else
532 {
533 $month = substr($bad_date, 0, 2);
534 $year = substr($bad_date, 2, 4);
535 }
Greg Akerf9168392011-08-29 19:29:05 -0500536
Kyle Farris896d95a2011-08-21 23:03:54 -0300537 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300538 }
539
540 // Date Like: YYYYMMDD
541 if (preg_match('/^\d{8}$/',$bad_date))
542 {
543 $month = substr($bad_date, 0, 2);
544 $day = substr($bad_date, 2, 2);
545 $year = substr($bad_date, 4, 4);
Greg Akerf9168392011-08-29 19:29:05 -0500546
Kyle Farris896d95a2011-08-21 23:03:54 -0300547 return date($format, strtotime($month . '/01/' . $year));
548 }
549
550 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
551 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
552 {
553 list($m, $d, $y) = explode('-', $bad_date);
554 return date($format, strtotime("{$y}-{$m}-{$d}"));
555 }
556
557 // Any other kind of string, when converted into UNIX time,
558 // produces "0 seconds after epoc..." is probably bad...
559 // return "Invalid Date".
560 if (date('U', strtotime($bad_date)) == '0')
561 {
562 return "Invalid Date";
563 }
564
565 // It's probably a valid-ish date format already
566 return date($format, strtotime($bad_date));
567 }
568}
569
570// ------------------------------------------------------------------------
571
572/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 * Timezone Menu
574 *
575 * Generates a drop-down menu of timezones.
576 *
577 * @access public
578 * @param string timezone
579 * @param string classname
580 * @param string menu name
581 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200582 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000583if ( ! function_exists('timezone_menu'))
584{
585 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
586 {
587 $CI =& get_instance();
588 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200589
Greg Akerc964e722011-08-29 19:31:29 -0500590 $default = ($default == 'GMT') ? 'UTC' : $default;
Derek Allard2067d1a2008-11-13 22:59:24 +0000591
592 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 if ($class != '')
595 {
596 $menu .= ' class="'.$class.'"';
597 }
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 foreach (timezones() as $key => $val)
602 {
603 $selected = ($default == $key) ? " selected='selected'" : '';
604 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
605 }
606
607 $menu .= "</select>";
608
609 return $menu;
610 }
611}
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613// ------------------------------------------------------------------------
614
615/**
616 * Timezones
617 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500618 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * for various other ones in this library
620 *
621 * @access public
622 * @param string timezone
623 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200624 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000625if ( ! function_exists('timezones'))
626{
627 function timezones($tz = '')
628 {
629 // Note: Don't change the order of these even though
630 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200631
632 $zones = array(
Greg Akerc964e722011-08-29 19:31:29 -0500633 'UM12' => -12,
634 'UM11' => -11,
635 'UM10' => -10,
636 'UM95' => -9.5,
637 'UM9' => -9,
638 'UM8' => -8,
639 'UM7' => -7,
640 'UM6' => -6,
641 'UM5' => -5,
642 'UM45' => -4.5,
643 'UM4' => -4,
644 'UM35' => -3.5,
645 'UM3' => -3,
646 'UM2' => -2,
647 'UM1' => -1,
648 'UTC' => 0,
649 'UP1' => +1,
650 'UP2' => +2,
651 'UP3' => +3,
652 'UP35' => +3.5,
653 'UP4' => +4,
654 'UP45' => +4.5,
655 'UP5' => +5,
656 'UP55' => +5.5,
657 'UP575' => +5.75,
658 'UP6' => +6,
659 'UP65' => +6.5,
660 'UP7' => +7,
661 'UP8' => +8,
662 'UP875' => +8.75,
663 'UP9' => +9,
664 'UP95' => +9.5,
665 'UP10' => +10,
666 'UP105' => +10.5,
667 'UP11' => +11,
668 'UP115' => +11.5,
669 'UP12' => +12,
670 'UP1275' => +12.75,
671 'UP13' => +13,
672 'UP14' => +14
673 );
Barry Mienydd671972010-10-04 16:33:58 +0200674
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 if ($tz == '')
676 {
677 return $zones;
678 }
Greg Akerf9168392011-08-29 19:29:05 -0500679
680 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
683 }
684}
685
Derek Allard2067d1a2008-11-13 22:59:24 +0000686/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000687/* Location: ./system/helpers/date_helper.php */