blob: e8a53035347f997fdec77825339a53b7047ab289 [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 }
57 else
58 {
59 return time();
60 }
61 }
62}
Barry Mienydd671972010-10-04 16:33:58 +020063
Derek Allard2067d1a2008-11-13 22:59:24 +000064// ------------------------------------------------------------------------
65
66/**
67 * Convert MySQL Style Datecodes
68 *
69 * This function is identical to PHPs date() function,
70 * except that it allows date codes to be formatted using
71 * the MySQL style, where each code letter is preceded
Derek Jones4b9c6292011-07-01 17:40:48 -050072 * with a percent sign: %Y %m %d etc...
Derek Allard2067d1a2008-11-13 22:59:24 +000073 *
74 * The benefit of doing dates this way is that you don't
75 * have to worry about escaping your text letters that
76 * match the date codes.
77 *
78 * @access public
79 * @param string
80 * @param integer
81 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +020082 */
Derek Allard2067d1a2008-11-13 22:59:24 +000083if ( ! function_exists('mdate'))
84{
85 function mdate($datestr = '', $time = '')
86 {
87 if ($datestr == '')
Greg Akerf9168392011-08-29 19:29:05 -050088 {
89 return '';
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -050093 {
94 $time = now();
95 }
Barry Mienydd671972010-10-04 16:33:58 +020096
Greg Akerf9168392011-08-29 19:29:05 -050097 $datestr = str_replace(
98 '%\\',
99 '',
100 preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr)
101 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 return date($datestr, $time);
103 }
104}
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106// ------------------------------------------------------------------------
107
108/**
109 * Standard Date
110 *
111 * Returns a date formatted according to the submitted standard.
112 *
113 * @access public
114 * @param string the chosen format
115 * @param integer Unix timestamp
116 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000118if ( ! function_exists('standard_date'))
119{
120 function standard_date($fmt = 'DATE_RFC822', $time = '')
121 {
122 $formats = array(
123 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
124 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
Greg Aker3b889a92011-01-10 12:35:57 -0600125 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%Q',
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
127 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
128 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
129 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
130 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
131 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
132 );
133
134 if ( ! isset($formats[$fmt]))
135 {
136 return FALSE;
137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 return mdate($formats[$fmt], $time);
140 }
141}
Barry Mienydd671972010-10-04 16:33:58 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143// ------------------------------------------------------------------------
144
145/**
146 * Timespan
147 *
148 * Returns a span of seconds in this format:
149 * 10 days 14 hours 36 minutes 47 seconds
150 *
151 * @access public
152 * @param integer a number of seconds
153 * @param integer Unix timestamp
154 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200155 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000156if ( ! function_exists('timespan'))
157{
158 function timespan($seconds = 1, $time = '')
159 {
160 $CI =& get_instance();
161 $CI->lang->load('date');
162
163 if ( ! is_numeric($seconds))
164 {
165 $seconds = 1;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 if ( ! is_numeric($time))
169 {
170 $time = time();
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Greg Akerf9168392011-08-29 19:29:05 -0500173 $seconds = ($time <= $seconds) ? 1 : $time - $seconds;
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 $str = '';
176 $years = floor($seconds / 31536000);
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if ($years > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200179 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200181 }
182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 $seconds -= $years * 31536000;
184 $months = floor($seconds / 2628000);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if ($years > 0 OR $months > 0)
187 {
188 if ($months > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200189 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
Barry Mienydd671972010-10-04 16:33:58 +0200191 }
192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 $seconds -= $months * 2628000;
194 }
195
196 $weeks = floor($seconds / 604800);
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 if ($years > 0 OR $months > 0 OR $weeks > 0)
199 {
200 if ($weeks > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200201 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $seconds -= $weeks * 604800;
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000207
208 $days = floor($seconds / 86400);
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 if ($months > 0 OR $weeks > 0 OR $days > 0)
211 {
212 if ($days > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200213 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 $seconds -= $days * 86400;
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $hours = floor($seconds / 3600);
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 if ($days > 0 OR $hours > 0)
223 {
224 if ($hours > 0)
225 {
226 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $seconds -= $hours * 3600;
230 }
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $minutes = floor($seconds / 60);
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 if ($days > 0 OR $hours > 0 OR $minutes > 0)
235 {
236 if ($minutes > 0)
Barry Mienydd671972010-10-04 16:33:58 +0200237 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 $seconds -= $minutes * 60;
242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 if ($str == '')
245 {
246 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 return substr(trim($str), 0, -1);
250 }
251}
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253// ------------------------------------------------------------------------
254
255/**
256 * Number of days in a month
257 *
258 * Takes a month/year as input and returns the number of days
259 * for the given month/year. Takes leap years into consideration.
260 *
261 * @access public
262 * @param integer a numeric month
263 * @param integer a numeric year
264 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200265 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000266if ( ! function_exists('days_in_month'))
267{
268 function days_in_month($month = 0, $year = '')
269 {
270 if ($month < 1 OR $month > 12)
271 {
272 return 0;
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 if ( ! is_numeric($year) OR strlen($year) != 4)
276 {
277 $year = date('Y');
278 }
Barry Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 if ($month == 2)
281 {
282 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
283 {
284 return 29;
285 }
286 }
287
288 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
289 return $days_in_month[$month - 1];
290 }
291}
Derek Jones36591092010-03-05 10:05:51 -0600292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293// ------------------------------------------------------------------------
294
295/**
296 * Converts a local Unix timestamp to GMT
297 *
298 * @access public
299 * @param integer Unix timestamp
300 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200301 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000302if ( ! function_exists('local_to_gmt'))
303{
304 function local_to_gmt($time = '')
305 {
306 if ($time == '')
Greg Akerf9168392011-08-29 19:29:05 -0500307 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 $time = time();
Greg Akerf9168392011-08-29 19:29:05 -0500309 }
310
311 return mktime(
312 gmdate("H", $time),
313 gmdate("i", $time),
314 gmdate("s", $time),
315 gmdate("m", $time),
316 gmdate("d", $time),
317 gmdate("Y", $time)
318 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320}
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322// ------------------------------------------------------------------------
323
324/**
325 * Converts GMT time to a localized value
326 *
327 * Takes a Unix timestamp (in GMT) as input, and returns
328 * at the local value based on the timezone and DST setting
329 * submitted
330 *
331 * @access public
332 * @param integer Unix timestamp
333 * @param string timezone
334 * @param bool whether DST is active
335 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200336 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000337if ( ! function_exists('gmt_to_local'))
338{
339 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
Barry Mienydd671972010-10-04 16:33:58 +0200340 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 if ($time == '')
342 {
343 return now();
344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 $time += timezones($timezone) * 3600;
347
348 if ($dst == TRUE)
349 {
350 $time += 3600;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 return $time;
354 }
355}
Derek Jones36591092010-03-05 10:05:51 -0600356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357// ------------------------------------------------------------------------
358
359/**
360 * Converts a MySQL Timestamp to Unix
361 *
362 * @access public
363 * @param integer Unix timestamp
364 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200365 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000366if ( ! function_exists('mysql_to_unix'))
367{
368 function mysql_to_unix($time = '')
369 {
370 // We'll remove certain characters for backward compatibility
371 // since the formatting changed with MySQL 4.1
372 // YYYY-MM-DD HH:MM:SS
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 $time = str_replace('-', '', $time);
375 $time = str_replace(':', '', $time);
376 $time = str_replace(' ', '', $time);
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 // YYYYMMDDHHMMSS
Derek Jones4b9c6292011-07-01 17:40:48 -0500379 return mktime(
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 substr($time, 8, 2),
381 substr($time, 10, 2),
382 substr($time, 12, 2),
383 substr($time, 4, 2),
384 substr($time, 6, 2),
385 substr($time, 0, 4)
386 );
387 }
388}
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390// ------------------------------------------------------------------------
391
392/**
393 * Unix to "Human"
394 *
395 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
396 *
397 * @access public
398 * @param integer Unix timestamp
399 * @param bool whether to show seconds
400 * @param string format: us or euro
401 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200402 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000403if ( ! function_exists('unix_to_human'))
404{
405 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
406 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500407 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ($fmt == 'us')
410 {
411 $r .= date('h', $time).':'.date('i', $time);
412 }
413 else
414 {
415 $r .= date('H', $time).':'.date('i', $time);
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if ($seconds)
419 {
420 $r .= ':'.date('s', $time);
421 }
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 if ($fmt == 'us')
424 {
425 $r .= ' '.date('A', $time);
426 }
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 return $r;
429 }
430}
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432// ------------------------------------------------------------------------
433
434/**
435 * Convert "human" date to GMT
436 *
437 * Reverses the above process
438 *
439 * @access public
440 * @param string format: us or euro
441 * @return integer
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000443if ( ! function_exists('human_to_unix'))
444{
445 function human_to_unix($datestr = '')
446 {
447 if ($datestr == '')
448 {
449 return FALSE;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 $datestr = trim($datestr);
Derek Jones36591092010-03-05 10:05:51 -0600453 $datestr = preg_replace("/\040+/", ' ', $datestr);
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 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))
456 {
457 return FALSE;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Jones36591092010-03-05 10:05:51 -0600460 $split = explode(' ', $datestr);
Derek Allard2067d1a2008-11-13 22:59:24 +0000461
462 $ex = explode("-", $split['0']);
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Jones4b9c6292011-07-01 17:40:48 -0500464 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
465 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
466 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000467
468 $ex = explode(":", $split['1']);
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
Derek Jones4b9c6292011-07-01 17:40:48 -0500471 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000472
Derek Jones1322ec52009-03-11 17:01:14 +0000473 if (isset($ex['2']) && preg_match('/[0-9]{1,2}/', $ex['2']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500475 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477 else
478 {
479 // Unless specified, seconds get set to zero.
480 $sec = '00';
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 if (isset($split['2']))
484 {
485 $ampm = strtolower($split['2']);
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
Greg Akerf9168392011-08-29 19:29:05 -0500488 {
489 $hour = $hour + 12;
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
Greg Akerf9168392011-08-29 19:29:05 -0500493 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500494 $hour = '00';
Greg Akerf9168392011-08-29 19:29:05 -0500495 }
496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 if (strlen($hour) == 1)
Greg Akerf9168392011-08-29 19:29:05 -0500498 {
499 $hour = '0'.$hour;
500 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 return mktime($hour, $min, $sec, $month, $day, $year);
504 }
505}
Barry Mienydd671972010-10-04 16:33:58 +0200506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507// ------------------------------------------------------------------------
508
509/**
Kyle Farris896d95a2011-08-21 23:03:54 -0300510 * Turns many "reasonably-date-like" strings into something
511 * that is actually useful. This only works for dates after unix epoch.
512 *
513 * @access public
514 * @param string The terribly formatted date-like string
515 * @param string Date format to return (same as php date function)
516 * @return string
517 */
518if ( ! function_exists('nice_date'))
519{
Greg Akerf9168392011-08-29 19:29:05 -0500520 function nice_date($bad_date = '', $format = FALSE)
Kyle Farris896d95a2011-08-21 23:03:54 -0300521 {
522 if (empty($bad_date))
523 {
524 return 'Unknown';
525 }
Greg Akerf9168392011-08-29 19:29:05 -0500526
Kyle Farris896d95a2011-08-21 23:03:54 -0300527 // Date like: YYYYMM
Greg Akerf9168392011-08-29 19:29:05 -0500528 if (preg_match('/^\d{6}$/', $bad_date))
Kyle Farris896d95a2011-08-21 23:03:54 -0300529 {
Kyle Farris896d95a2011-08-21 23:03:54 -0300530 if (in_array(substr($bad_date, 0, 2),array('19', '20')))
531 {
532 $year = substr($bad_date, 0, 4);
533 $month = substr($bad_date, 4, 2);
534 }
535 else
536 {
537 $month = substr($bad_date, 0, 2);
538 $year = substr($bad_date, 2, 4);
539 }
Greg Akerf9168392011-08-29 19:29:05 -0500540
Kyle Farris896d95a2011-08-21 23:03:54 -0300541 return date($format, strtotime($year . '-' . $month . '-01'));
Kyle Farris896d95a2011-08-21 23:03:54 -0300542 }
543
544 // Date Like: YYYYMMDD
545 if (preg_match('/^\d{8}$/',$bad_date))
546 {
547 $month = substr($bad_date, 0, 2);
548 $day = substr($bad_date, 2, 2);
549 $year = substr($bad_date, 4, 4);
Greg Akerf9168392011-08-29 19:29:05 -0500550
Kyle Farris896d95a2011-08-21 23:03:54 -0300551 return date($format, strtotime($month . '/01/' . $year));
552 }
553
554 // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between)
555 if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date))
556 {
557 list($m, $d, $y) = explode('-', $bad_date);
558 return date($format, strtotime("{$y}-{$m}-{$d}"));
559 }
560
561 // Any other kind of string, when converted into UNIX time,
562 // produces "0 seconds after epoc..." is probably bad...
563 // return "Invalid Date".
564 if (date('U', strtotime($bad_date)) == '0')
565 {
566 return "Invalid Date";
567 }
568
569 // It's probably a valid-ish date format already
570 return date($format, strtotime($bad_date));
571 }
572}
573
574// ------------------------------------------------------------------------
575
576/**
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * Timezone Menu
578 *
579 * Generates a drop-down menu of timezones.
580 *
581 * @access public
582 * @param string timezone
583 * @param string classname
584 * @param string menu name
585 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200586 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000587if ( ! function_exists('timezone_menu'))
588{
589 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
590 {
591 $CI =& get_instance();
592 $CI->lang->load('date');
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 if ($default == 'GMT')
595 $default = 'UTC';
596
597 $menu = '<select name="'.$name.'"';
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 if ($class != '')
600 {
601 $menu .= ' class="'.$class.'"';
602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 $menu .= ">\n";
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 foreach (timezones() as $key => $val)
607 {
608 $selected = ($default == $key) ? " selected='selected'" : '';
609 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
610 }
611
612 $menu .= "</select>";
613
614 return $menu;
615 }
616}
Barry Mienydd671972010-10-04 16:33:58 +0200617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618// ------------------------------------------------------------------------
619
620/**
621 * Timezones
622 *
Derek Jones4b9c6292011-07-01 17:40:48 -0500623 * Returns an array of timezones. This is a helper function
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 * for various other ones in this library
625 *
626 * @access public
627 * @param string timezone
628 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200629 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000630if ( ! function_exists('timezones'))
631{
632 function timezones($tz = '')
633 {
634 // Note: Don't change the order of these even though
635 // some items appear to be in the wrong order
Barry Mienydd671972010-10-04 16:33:58 +0200636
637 $zones = array(
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 'UM12' => -12,
639 'UM11' => -11,
640 'UM10' => -10,
641 'UM95' => -9.5,
642 'UM9' => -9,
643 'UM8' => -8,
644 'UM7' => -7,
645 'UM6' => -6,
646 'UM5' => -5,
647 'UM45' => -4.5,
648 'UM4' => -4,
649 'UM35' => -3.5,
650 'UM3' => -3,
651 'UM2' => -2,
652 'UM1' => -1,
653 'UTC' => 0,
654 'UP1' => +1,
655 'UP2' => +2,
656 'UP3' => +3,
657 'UP35' => +3.5,
658 'UP4' => +4,
659 'UP45' => +4.5,
660 'UP5' => +5,
661 'UP55' => +5.5,
662 'UP575' => +5.75,
663 'UP6' => +6,
664 'UP65' => +6.5,
665 'UP7' => +7,
666 'UP8' => +8,
667 'UP875' => +8.75,
668 'UP9' => +9,
669 'UP95' => +9.5,
670 'UP10' => +10,
671 'UP105' => +10.5,
672 'UP11' => +11,
673 'UP115' => +11.5,
674 'UP12' => +12,
675 'UP1275' => +12.75,
676 'UP13' => +13,
677 'UP14' => +14
678 );
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 if ($tz == '')
681 {
682 return $zones;
683 }
Greg Akerf9168392011-08-29 19:29:05 -0500684
685 $tz = ($tz == 'GMT') ? 'UTC' : $tz;
686
Derek Allard2067d1a2008-11-13 22:59:24 +0000687 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
688 }
689}
690
Derek Allard2067d1a2008-11-13 22:59:24 +0000691/* End of file date_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000692/* Location: ./system/helpers/date_helper.php */