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