blob: b8df03edbed2039c2312c3900144881ef3dc3c9b [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Date Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @link http://www.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
37 */
38function now()
39{
admin75198f92006-10-07 03:16:53 +000040 $CI =& get_instance();
adminb0dd10f2006-08-25 17:25:49 +000041
admine334c472006-10-21 19:44:22 +000042 if (strtolower($CI->config->item('time_reference')) == 'gmt')
adminb0dd10f2006-08-25 17:25:49 +000043 {
admine334c472006-10-21 19:44:22 +000044 $now = time();
45 $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
adminb0dd10f2006-08-25 17:25:49 +000046
47 if (strlen($system_time) < 10)
48 {
49 $system_time = time();
50 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
51 }
52
53 return $system_time;
54 }
55 else
56 {
57 return time();
58 }
59}
60
61// ------------------------------------------------------------------------
62
63/**
64 * Convert MySQL Style Datecodes
65 *
66 * This function is identical to PHPs date() function,
67 * except that it allows date codes to be formatted using
68 * the MySQL style, where each code letter is preceded
69 * with a percent sign: %Y %m %d etc...
70 *
71 * The benefit of doing dates this way is that you don't
72 * have to worry about escaping your text letters that
73 * match the date codes.
74 *
75 * @access public
76 * @param string
77 * @param integer
78 * @return integer
79 */
80function mdate($datestr = '', $time = '')
81{
82 if ($datestr == '')
83 return '';
84
85 if ($time == '')
86 $time = now();
87
88 $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
89 return date($datestr, $time);
90}
91
92// ------------------------------------------------------------------------
93
94/**
admine8f6eb62006-10-02 06:46:16 +000095 * Standard Date
96 *
97 * Returns a date formatted according to the submitted standard.
98 *
99 * @access public
100 * @param string the chosen format
101 * @param integer Unix timestamp
102 * @return string
103 */
104function standard_date($fmt = 'DATE_RFC822', $time = '')
105{
106 $formats = array(
107 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
108 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
109 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
110 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
111 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
112 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
113 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
114 'DATE_RFC2822' => '%D, %d %M %Y %H:%i:%s %O',
115 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
116 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
117 );
118
119 if ( ! isset($formats[$fmt]))
120 {
121 return FALSE;
122 }
123
124 return mdate($formats[$fmt], $time);
125}
126
127
128// ------------------------------------------------------------------------
129
130/**
131 * Timespan
adminb0dd10f2006-08-25 17:25:49 +0000132 *
admine334c472006-10-21 19:44:22 +0000133 * Returns a span of seconds in this format:
adminb0dd10f2006-08-25 17:25:49 +0000134 * 10 days 14 hours 36 minutes 47 seconds
135 *
136 * @access public
137 * @param integer a number of seconds
138 * @param integer Unix timestamp
139 * @return integer
140 */
141function timespan($seconds = 1, $time = '')
142{
admin75198f92006-10-07 03:16:53 +0000143 $CI =& get_instance();
144 $CI->lang->load('date');
adminb0dd10f2006-08-25 17:25:49 +0000145
146 if ( ! is_numeric($seconds))
147 {
148 $seconds = 1;
149 }
150
151 if ( ! is_numeric($time))
152 {
153 $time = time();
154 }
155
156 if ($time <= $seconds)
157 {
158 $seconds = 1;
159 }
160 else
161 {
162 $seconds = $time - $seconds;
163 }
164
165 $str = '';
166 $years = floor($seconds / 31536000);
167
168 if ($years > 0)
169 {
admin75198f92006-10-07 03:16:53 +0000170 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000171 }
172
173 $seconds -= $years * 31536000;
174 $months = floor($seconds / 2628000);
175
176 if ($years > 0 OR $months > 0)
177 {
178 if ($months > 0)
179 {
admin75198f92006-10-07 03:16:53 +0000180 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000181 }
182
183 $seconds -= $months * 2628000;
184 }
185
186 $weeks = floor($seconds / 604800);
187
188 if ($years > 0 OR $months > 0 OR $weeks > 0)
189 {
190 if ($weeks > 0)
191 {
admin75198f92006-10-07 03:16:53 +0000192 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000193 }
194
195 $seconds -= $weeks * 604800;
196 }
197
198 $days = floor($seconds / 86400);
199
200 if ($months > 0 OR $weeks > 0 OR $days > 0)
201 {
202 if ($days > 0)
203 {
admin75198f92006-10-07 03:16:53 +0000204 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000205 }
206
207 $seconds -= $days * 86400;
208 }
209
210 $hours = floor($seconds / 3600);
211
212 if ($days > 0 OR $hours > 0)
213 {
214 if ($hours > 0)
215 {
admin75198f92006-10-07 03:16:53 +0000216 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000217 }
218
219 $seconds -= $hours * 3600;
220 }
221
222 $minutes = floor($seconds / 60);
223
224 if ($days > 0 OR $hours > 0 OR $minutes > 0)
225 {
226 if ($minutes > 0)
227 {
admin75198f92006-10-07 03:16:53 +0000228 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000229 }
230
231 $seconds -= $minutes * 60;
232 }
233
234 if ($str == '')
235 {
admin75198f92006-10-07 03:16:53 +0000236 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
adminb0dd10f2006-08-25 17:25:49 +0000237 }
238
239 return substr(trim($str), 0, -1);
240}
241
242// ------------------------------------------------------------------------
243
244/**
245 * Number of days in a month
246 *
admine334c472006-10-21 19:44:22 +0000247 * Takes a month/year as input and returns the number of days
adminb0dd10f2006-08-25 17:25:49 +0000248 * for the given month/year. Takes leap years into consideration.
249 *
250 * @access public
251 * @param integer a numeric month
252 * @param integer a numeric year
253 * @return integer
254 */
255function days_in_month($month = 0, $year = '')
256{
257 if ($month < 1 OR $month > 12)
258 {
259 return 0;
260 }
261
admin1cf89aa2006-09-03 18:24:39 +0000262 if ( ! is_numeric($year) OR strlen($year) != 4)
adminb0dd10f2006-08-25 17:25:49 +0000263 {
264 $year = date('Y');
265 }
266
267 if ($month == 2)
admine334c472006-10-21 19:44:22 +0000268 {
adminb0dd10f2006-08-25 17:25:49 +0000269 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
270 {
271 return 29;
272 }
273 }
274
275 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
276 return $days_in_month[$month - 1];
277}
278
279// ------------------------------------------------------------------------
280
281/**
282 * Converts a local Unix timestamp to GMT
283 *
284 * @access public
285 * @param integer Unix timestamp
286 * @return integer
287 */
288function local_to_gmt($time = '')
289{
290 if ($time == '')
291 $time = time();
292
admine334c472006-10-21 19:44:22 +0000293 return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
adminb0dd10f2006-08-25 17:25:49 +0000294}
295
296// ------------------------------------------------------------------------
297
298/**
299 * Converts GMT time to a localized value
300 *
301 * Takes a Unix timestamp (in GMT) as input, and returns
302 * at the local value based on the timezone and DST setting
303 * submitted
304 *
305 * @access public
306 * @param integer Unix timestamp
307 * @param string timezone
308 * @param bool whether DST is active
309 * @return integer
310 */
311function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
312{
313 if ($time == '')
314 {
315 return now();
316 }
317
318 $time += timezones($timezone) * 3600;
319
320 if ($dst == TRUE)
321 {
322 $time += 3600;
323 }
324
325 return $time;
326}
327
328// ------------------------------------------------------------------------
329
330/**
331 * Converts a MySQL Timestamp to Unix
332 *
333 * @access public
334 * @param integer Unix timestamp
335 * @return integer
336 */
337function mysql_to_unix($time = '')
admine334c472006-10-21 19:44:22 +0000338{
adminb0dd10f2006-08-25 17:25:49 +0000339 // We'll remove certain characters for backward compatibility
340 // since the formatting changed with MySQL 4.1
341 // YYYY-MM-DD HH:MM:SS
342
343 $time = str_replace('-', '', $time);
344 $time = str_replace(':', '', $time);
345 $time = str_replace(' ', '', $time);
346
347 // YYYYMMDDHHMMSS
admine334c472006-10-21 19:44:22 +0000348 return mktime(
adminb0dd10f2006-08-25 17:25:49 +0000349 substr($time, 8, 2),
350 substr($time, 10, 2),
351 substr($time, 12, 2),
352 substr($time, 4, 2),
353 substr($time, 6, 2),
354 substr($time, 0, 4)
355 );
356}
357
358// ------------------------------------------------------------------------
359
360/**
361 * Unix to "Human"
362 *
363 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
364 *
365 * @access public
366 * @param integer Unix timestamp
367 * @param bool whether to show seconds
368 * @param string format: us or euro
369 * @return string
370 */
371function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
372{
373 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
374
375 if ($fmt == 'us')
376 {
377 $r .= date('h', $time).':'.date('i', $time);
378 }
379 else
380 {
381 $r .= date('H', $time).':'.date('i', $time);
382 }
383
384 if ($seconds)
385 {
386 $r .= ':'.date('s', $time);
387 }
388
389 if ($fmt == 'us')
390 {
391 $r .= ' '.date('A', $time);
392 }
393
394 return $r;
395}
396
397// ------------------------------------------------------------------------
398
399/**
400 * Convert "human" date to GMT
401 *
402 * Reverses the above process
403 *
404 * @access public
405 * @param string format: us or euro
406 * @return integer
407 */
408function human_to_unix($datestr = '')
409{
410 if ($datestr == '')
411 {
412 return FALSE;
413 }
414
415 $datestr = trim($datestr);
416 $datestr = preg_replace("/\040+/", "\040", $datestr);
417
418 if ( ! ereg("^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\040[0-9]{1,2}:[0-9]{1,2}.*$", $datestr))
419 {
420 return FALSE;
421 }
422
423 $split = preg_split("/\040/", $datestr);
424
admine334c472006-10-21 19:44:22 +0000425 $ex = explode("-", $split['0']);
adminb0dd10f2006-08-25 17:25:49 +0000426
427 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
428 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
429 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
430
admine334c472006-10-21 19:44:22 +0000431 $ex = explode(":", $split['1']);
adminb0dd10f2006-08-25 17:25:49 +0000432
433 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
434 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
435
436 if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
437 {
438 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
439 }
440 else
441 {
442 // Unless specified, seconds get set to zero.
443 $sec = '00';
444 }
445
446 if (isset($split['2']))
447 {
448 $ampm = strtolower($split['2']);
449
450 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
451 $hour = $hour + 12;
452
453 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
454 $hour = '00';
455
456 if (strlen($hour) == 1)
457 $hour = '0'.$hour;
458 }
459
460 return mktime($hour, $min, $sec, $month, $day, $year);
461}
462
463// ------------------------------------------------------------------------
464
465/**
466 * Timezone Menu
467 *
468 * Generates a drop-down menu of timezones.
469 *
470 * @access public
471 * @param string timezone
472 * @param string classname
473 * @param string menu name
474 * @return string
475 */
476function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
477{
admin75198f92006-10-07 03:16:53 +0000478 $CI =& get_instance();
479 $CI->lang->load('date');
adminb0dd10f2006-08-25 17:25:49 +0000480
481 if ($default == 'GMT')
482 $default = 'UTC';
483
484 $menu = '<select name="'.$name.'"';
485
486 if ($class != '')
487 {
488 $menu .= ' class="'.$class.'"';
489 }
490
491 $menu .= ">\n";
492
493 foreach (timezones() as $key => $val)
494 {
495 $selected = ($default == $key) ? " selected='selected'" : '';
admin75198f92006-10-07 03:16:53 +0000496 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
adminb0dd10f2006-08-25 17:25:49 +0000497 }
498
499 $menu .= "</select>";
500
501 return $menu;
502}
503
504// ------------------------------------------------------------------------
505
506/**
507 * Timezones
508 *
509 * Returns an array of timezones. This is a helper function
adminfafe28b2006-10-21 19:08:17 +0000510 * for various other ones in this library
adminb0dd10f2006-08-25 17:25:49 +0000511 *
512 * @access public
513 * @param string timezone
514 * @return string
515 */
516function timezones($tz = '')
517{
admine334c472006-10-21 19:44:22 +0000518 // Note: Don't change the order of these even though
adminb0dd10f2006-08-25 17:25:49 +0000519 // some items appear to be in the wrong order
520
admine334c472006-10-21 19:44:22 +0000521 $zones = array(
adminb0dd10f2006-08-25 17:25:49 +0000522 'UM12' => -12,
523 'UM11' => -11,
524 'UM10' => -10,
525 'UM9' => -9,
526 'UM8' => -8,
527 'UM7' => -7,
528 'UM6' => -6,
529 'UM5' => -5,
530 'UM4' => -4,
531 'UM25' => -2.5,
532 'UM3' => -3,
533 'UM2' => -2,
534 'UM1' => -1,
535 'UTC' => 0,
536 'UP1' => +1,
537 'UP2' => +2,
538 'UP3' => +3,
539 'UP25' => +2.5,
540 'UP4' => +4,
541 'UP35' => +3.5,
542 'UP5' => +5,
543 'UP45' => +4.5,
544 'UP6' => +6,
545 'UP7' => +7,
546 'UP8' => +8,
547 'UP9' => +9,
548 'UP85' => +8.5,
549 'UP10' => +10,
550 'UP11' => +11,
admine334c472006-10-21 19:44:22 +0000551 'UP12' => +12
adminb0dd10f2006-08-25 17:25:49 +0000552 );
553
554 if ($tz == '')
555 {
556 return $zones;
557 }
558
559 if ($tz == 'GMT')
560 $tz = 'UTC';
561
562 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
563}
564
565
566?>