1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
19 * CodeIgniter Date Helpers
\r
21 * @package CodeIgniter
\r
22 * @subpackage Helpers
\r
24 * @author ExpressionEngine Dev Team
\r
25 * @link http://codeigniter.com/user_guide/helpers/date_helper.html
\r
28 // ------------------------------------------------------------------------
\r
33 * Returns time() or its GMT equivalent based on the config file preference
\r
38 if ( ! function_exists('now'))
\r
42 $CI =& get_instance();
\r
44 if (strtolower($CI->config->item('time_reference')) == 'gmt')
\r
47 $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
\r
49 if (strlen($system_time) < 10)
\r
51 $system_time = time();
\r
52 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
\r
55 return $system_time;
\r
64 // ------------------------------------------------------------------------
\r
67 * Convert MySQL Style Datecodes
\r
69 * This function is identical to PHPs date() function,
\r
70 * except that it allows date codes to be formatted using
\r
71 * the MySQL style, where each code letter is preceded
\r
72 * with a percent sign: %Y %m %d etc...
\r
74 * The benefit of doing dates this way is that you don't
\r
75 * have to worry about escaping your text letters that
\r
76 * match the date codes.
\r
83 if ( ! function_exists('mdate'))
\r
85 function mdate($datestr = '', $time = '')
\r
93 $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
\r
94 return date($datestr, $time);
\r
98 // ------------------------------------------------------------------------
\r
103 * Returns a date formatted according to the submitted standard.
\r
106 * @param string the chosen format
\r
107 * @param integer Unix timestamp
\r
110 if ( ! function_exists('standard_date'))
\r
112 function standard_date($fmt = 'DATE_RFC822', $time = '')
\r
115 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
\r
116 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
\r
117 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
\r
118 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
\r
119 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
\r
120 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
\r
121 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
\r
122 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
\r
123 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
\r
126 if ( ! isset($formats[$fmt]))
\r
131 return mdate($formats[$fmt], $time);
\r
135 // ------------------------------------------------------------------------
\r
140 * Returns a span of seconds in this format:
\r
141 * 10 days 14 hours 36 minutes 47 seconds
\r
144 * @param integer a number of seconds
\r
145 * @param integer Unix timestamp
\r
148 if ( ! function_exists('timespan'))
\r
150 function timespan($seconds = 1, $time = '')
\r
152 $CI =& get_instance();
\r
153 $CI->lang->load('date');
\r
155 if ( ! is_numeric($seconds))
\r
160 if ( ! is_numeric($time))
\r
165 if ($time <= $seconds)
\r
171 $seconds = $time - $seconds;
\r
175 $years = floor($seconds / 31536000);
\r
179 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
\r
182 $seconds -= $years * 31536000;
\r
183 $months = floor($seconds / 2628000);
\r
185 if ($years > 0 OR $months > 0)
\r
189 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
\r
192 $seconds -= $months * 2628000;
\r
195 $weeks = floor($seconds / 604800);
\r
197 if ($years > 0 OR $months > 0 OR $weeks > 0)
\r
201 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
\r
204 $seconds -= $weeks * 604800;
\r
207 $days = floor($seconds / 86400);
\r
209 if ($months > 0 OR $weeks > 0 OR $days > 0)
\r
213 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
\r
216 $seconds -= $days * 86400;
\r
219 $hours = floor($seconds / 3600);
\r
221 if ($days > 0 OR $hours > 0)
\r
225 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
\r
228 $seconds -= $hours * 3600;
\r
231 $minutes = floor($seconds / 60);
\r
233 if ($days > 0 OR $hours > 0 OR $minutes > 0)
\r
237 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
\r
240 $seconds -= $minutes * 60;
\r
245 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
\r
248 return substr(trim($str), 0, -1);
\r
252 // ------------------------------------------------------------------------
\r
255 * Number of days in a month
\r
257 * Takes a month/year as input and returns the number of days
\r
258 * for the given month/year. Takes leap years into consideration.
\r
261 * @param integer a numeric month
\r
262 * @param integer a numeric year
\r
265 if ( ! function_exists('days_in_month'))
\r
267 function days_in_month($month = 0, $year = '')
\r
269 if ($month < 1 OR $month > 12)
\r
274 if ( ! is_numeric($year) OR strlen($year) != 4)
\r
281 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
\r
287 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
\r
288 return $days_in_month[$month - 1];
\r
292 // ------------------------------------------------------------------------
\r
295 * Converts a local Unix timestamp to GMT
\r
298 * @param integer Unix timestamp
\r
301 if ( ! function_exists('local_to_gmt'))
\r
303 function local_to_gmt($time = '')
\r
308 return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
\r
312 // ------------------------------------------------------------------------
\r
315 * Converts GMT time to a localized value
\r
317 * Takes a Unix timestamp (in GMT) as input, and returns
\r
318 * at the local value based on the timezone and DST setting
\r
322 * @param integer Unix timestamp
\r
323 * @param string timezone
\r
324 * @param bool whether DST is active
\r
327 if ( ! function_exists('gmt_to_local'))
\r
329 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
\r
336 $time += timezones($timezone) * 3600;
\r
347 // ------------------------------------------------------------------------
\r
350 * Converts a MySQL Timestamp to Unix
\r
353 * @param integer Unix timestamp
\r
356 if ( ! function_exists('mysql_to_unix'))
\r
358 function mysql_to_unix($time = '')
\r
360 // We'll remove certain characters for backward compatibility
\r
361 // since the formatting changed with MySQL 4.1
\r
362 // YYYY-MM-DD HH:MM:SS
\r
364 $time = str_replace('-', '', $time);
\r
365 $time = str_replace(':', '', $time);
\r
366 $time = str_replace(' ', '', $time);
\r
370 substr($time, 8, 2),
\r
371 substr($time, 10, 2),
\r
372 substr($time, 12, 2),
\r
373 substr($time, 4, 2),
\r
374 substr($time, 6, 2),
\r
375 substr($time, 0, 4)
\r
380 // ------------------------------------------------------------------------
\r
385 * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
\r
388 * @param integer Unix timestamp
\r
389 * @param bool whether to show seconds
\r
390 * @param string format: us or euro
\r
393 if ( ! function_exists('unix_to_human'))
\r
395 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
\r
397 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
\r
401 $r .= date('h', $time).':'.date('i', $time);
\r
405 $r .= date('H', $time).':'.date('i', $time);
\r
410 $r .= ':'.date('s', $time);
\r
415 $r .= ' '.date('A', $time);
\r
422 // ------------------------------------------------------------------------
\r
425 * Convert "human" date to GMT
\r
427 * Reverses the above process
\r
430 * @param string format: us or euro
\r
433 if ( ! function_exists('human_to_unix'))
\r
435 function human_to_unix($datestr = '')
\r
437 if ($datestr == '')
\r
442 $datestr = trim($datestr);
\r
443 $datestr = preg_replace("/\040+/", "\040", $datestr);
\r
445 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))
\r
450 $split = preg_split("/\040/", $datestr);
\r
452 $ex = explode("-", $split['0']);
\r
454 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
\r
455 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
\r
456 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
\r
458 $ex = explode(":", $split['1']);
\r
460 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
\r
461 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
\r
463 if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
\r
465 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
\r
469 // Unless specified, seconds get set to zero.
\r
473 if (isset($split['2']))
\r
475 $ampm = strtolower($split['2']);
\r
477 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
\r
478 $hour = $hour + 12;
\r
480 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
\r
483 if (strlen($hour) == 1)
\r
487 return mktime($hour, $min, $sec, $month, $day, $year);
\r
491 // ------------------------------------------------------------------------
\r
496 * Generates a drop-down menu of timezones.
\r
499 * @param string timezone
\r
500 * @param string classname
\r
501 * @param string menu name
\r
504 if ( ! function_exists('timezone_menu'))
\r
506 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
\r
508 $CI =& get_instance();
\r
509 $CI->lang->load('date');
\r
511 if ($default == 'GMT')
\r
514 $menu = '<select name="'.$name.'"';
\r
518 $menu .= ' class="'.$class.'"';
\r
523 foreach (timezones() as $key => $val)
\r
525 $selected = ($default == $key) ? " selected='selected'" : '';
\r
526 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
\r
529 $menu .= "</select>";
\r
535 // ------------------------------------------------------------------------
\r
540 * Returns an array of timezones. This is a helper function
\r
541 * for various other ones in this library
\r
544 * @param string timezone
\r
547 if ( ! function_exists('timezones'))
\r
549 function timezones($tz = '')
\r
551 // Note: Don't change the order of these even though
\r
552 // some items appear to be in the wrong order
\r
592 'UP1275' => +12.75,
\r
605 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
\r
610 /* End of file date_helper.php */
\r
611 /* Location: ./system/helpers/date_helper.php */