Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 19 | * CodeIgniter Date Helpers
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 20 | *
|
| 21 | * @package CodeIgniter
|
| 22 | * @subpackage Helpers
|
| 23 | * @category Helpers
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/helpers/date_helper.html
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 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 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 38 | if (! function_exists('now'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 39 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 40 | function now()
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 41 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 42 | $CI =& get_instance();
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 43 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 44 | if (strtolower($CI->config->item('time_reference')) == 'gmt')
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 45 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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));
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 48 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 49 | 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 | }
|
| 54 |
|
| 55 | return $system_time;
|
| 56 | }
|
| 57 | else
|
| 58 | {
|
| 59 | return time();
|
| 60 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | // ------------------------------------------------------------------------
|
| 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
|
| 72 | * with a percent sign: %Y %m %d etc...
|
| 73 | *
|
| 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
|
| 82 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 83 | if (! function_exists('mdate'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 84 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 85 | function mdate($datestr = '', $time = '')
|
| 86 | {
|
| 87 | if ($datestr == '')
|
| 88 | return '';
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 89 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 90 | if ($time == '')
|
| 91 | $time = now();
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 92 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 93 | $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
|
| 94 | return date($datestr, $time);
|
| 95 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 96 | }
|
| 97 |
|
| 98 | // ------------------------------------------------------------------------
|
| 99 |
|
| 100 | /**
|
| 101 | * Standard Date
|
| 102 | *
|
| 103 | * Returns a date formatted according to the submitted standard.
|
| 104 | *
|
| 105 | * @access public
|
| 106 | * @param string the chosen format
|
| 107 | * @param integer Unix timestamp
|
| 108 | * @return string
|
| 109 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 110 | if (! function_exists('standard_date'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 111 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 112 | function standard_date($fmt = 'DATE_RFC822', $time = '')
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 113 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 114 | $formats = array(
|
| 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%O',
|
| 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'
|
| 124 | );
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 125 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 126 | if ( ! isset($formats[$fmt]))
|
| 127 | {
|
| 128 | return FALSE;
|
| 129 | }
|
| 130 |
|
| 131 | return mdate($formats[$fmt], $time);
|
| 132 | }
|
| 133 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 134 |
|
| 135 | // ------------------------------------------------------------------------
|
| 136 |
|
| 137 | /**
|
| 138 | * Timespan
|
| 139 | *
|
| 140 | * Returns a span of seconds in this format:
|
| 141 | * 10 days 14 hours 36 minutes 47 seconds
|
| 142 | *
|
| 143 | * @access public
|
| 144 | * @param integer a number of seconds
|
| 145 | * @param integer Unix timestamp
|
| 146 | * @return integer
|
| 147 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 148 | if (! function_exists('timespan'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 149 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 150 | function timespan($seconds = 1, $time = '')
|
| 151 | {
|
| 152 | $CI =& get_instance();
|
| 153 | $CI->lang->load('date');
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 154 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 155 | if ( ! is_numeric($seconds))
|
| 156 | {
|
| 157 | $seconds = 1;
|
| 158 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 159 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 160 | if ( ! is_numeric($time))
|
| 161 | {
|
| 162 | $time = time();
|
| 163 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 164 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 165 | if ($time <= $seconds)
|
| 166 | {
|
| 167 | $seconds = 1;
|
| 168 | }
|
| 169 | else
|
| 170 | {
|
| 171 | $seconds = $time - $seconds;
|
| 172 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 173 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 174 | $str = '';
|
| 175 | $years = floor($seconds / 31536000);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 176 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 177 | if ($years > 0)
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 178 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 179 | $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 180 | }
|
| 181 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 182 | $seconds -= $years * 31536000;
|
| 183 | $months = floor($seconds / 2628000);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 184 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 185 | if ($years > 0 OR $months > 0)
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 186 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 187 | if ($months > 0)
|
| 188 | {
|
| 189 | $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
|
| 190 | }
|
| 191 |
|
| 192 | $seconds -= $months * 2628000;
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 193 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 194 |
|
| 195 | $weeks = floor($seconds / 604800);
|
| 196 |
|
| 197 | if ($years > 0 OR $months > 0 OR $weeks > 0)
|
| 198 | {
|
| 199 | if ($weeks > 0)
|
| 200 | {
|
| 201 | $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
|
| 202 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 203 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 204 | $seconds -= $weeks * 604800;
|
| 205 | }
|
| 206 |
|
| 207 | $days = floor($seconds / 86400);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 208 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 209 | if ($months > 0 OR $weeks > 0 OR $days > 0)
|
| 210 | {
|
| 211 | if ($days > 0)
|
| 212 | {
|
| 213 | $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
|
| 214 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 215 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 216 | $seconds -= $days * 86400;
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 217 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 218 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 219 | $hours = floor($seconds / 3600);
|
| 220 |
|
| 221 | if ($days > 0 OR $hours > 0)
|
| 222 | {
|
| 223 | if ($hours > 0)
|
| 224 | {
|
| 225 | $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
|
| 226 | }
|
| 227 |
|
| 228 | $seconds -= $hours * 3600;
|
| 229 | }
|
| 230 |
|
| 231 | $minutes = floor($seconds / 60);
|
| 232 |
|
| 233 | if ($days > 0 OR $hours > 0 OR $minutes > 0)
|
| 234 | {
|
| 235 | if ($minutes > 0)
|
| 236 | {
|
| 237 | $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
|
| 238 | }
|
| 239 |
|
| 240 | $seconds -= $minutes * 60;
|
| 241 | }
|
| 242 |
|
| 243 | if ($str == '')
|
| 244 | {
|
| 245 | $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
|
| 246 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 247 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 248 | return substr(trim($str), 0, -1);
|
| 249 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 250 | }
|
| 251 |
|
| 252 | // ------------------------------------------------------------------------
|
| 253 |
|
| 254 | /**
|
| 255 | * Number of days in a month
|
| 256 | *
|
| 257 | * Takes a month/year as input and returns the number of days
|
| 258 | * for the given month/year. Takes leap years into consideration.
|
| 259 | *
|
| 260 | * @access public
|
| 261 | * @param integer a numeric month
|
| 262 | * @param integer a numeric year
|
| 263 | * @return integer
|
| 264 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 265 | if (! function_exists('days_in_month'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 266 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 267 | function days_in_month($month = 0, $year = '')
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 268 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 269 | if ($month < 1 OR $month > 12)
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 270 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 271 | return 0;
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 272 | }
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 273 |
|
| 274 | if ( ! is_numeric($year) OR strlen($year) != 4)
|
| 275 | {
|
| 276 | $year = date('Y');
|
| 277 | }
|
| 278 |
|
| 279 | if ($month == 2)
|
| 280 | {
|
| 281 | if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
|
| 282 | {
|
| 283 | return 29;
|
| 284 | }
|
| 285 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 286 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 287 | $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
|
| 288 | return $days_in_month[$month - 1];
|
| 289 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 290 | }
|
| 291 |
|
| 292 | // ------------------------------------------------------------------------
|
| 293 |
|
| 294 | /**
|
| 295 | * Converts a local Unix timestamp to GMT
|
| 296 | *
|
| 297 | * @access public
|
| 298 | * @param integer Unix timestamp
|
| 299 | * @return integer
|
| 300 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 301 | if (! function_exists('local_to_gmt'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 302 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 303 | function local_to_gmt($time = '')
|
| 304 | {
|
| 305 | if ($time == '')
|
| 306 | $time = time();
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 307 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 308 | return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
|
| 309 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 310 | }
|
| 311 |
|
| 312 | // ------------------------------------------------------------------------
|
| 313 |
|
| 314 | /**
|
| 315 | * Converts GMT time to a localized value
|
| 316 | *
|
| 317 | * Takes a Unix timestamp (in GMT) as input, and returns
|
| 318 | * at the local value based on the timezone and DST setting
|
| 319 | * submitted
|
| 320 | *
|
| 321 | * @access public
|
| 322 | * @param integer Unix timestamp
|
| 323 | * @param string timezone
|
| 324 | * @param bool whether DST is active
|
| 325 | * @return integer
|
| 326 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 327 | if (! function_exists('gmt_to_local'))
|
| 328 | {
|
| 329 | function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
|
| 330 | {
|
| 331 | if ($time == '')
|
| 332 | {
|
| 333 | return now();
|
| 334 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 335 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 336 | $time += timezones($timezone) * 3600;
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 337 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 338 | if ($dst == TRUE)
|
| 339 | {
|
| 340 | $time += 3600;
|
| 341 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 342 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 343 | return $time;
|
| 344 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 345 | }
|
| 346 |
|
| 347 | // ------------------------------------------------------------------------
|
| 348 |
|
| 349 | /**
|
| 350 | * Converts a MySQL Timestamp to Unix
|
| 351 | *
|
| 352 | * @access public
|
| 353 | * @param integer Unix timestamp
|
| 354 | * @return integer
|
| 355 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 356 | if (! function_exists('mysql_to_unix'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 357 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 358 | function mysql_to_unix($time = '')
|
| 359 | {
|
| 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
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 363 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 364 | $time = str_replace('-', '', $time);
|
| 365 | $time = str_replace(':', '', $time);
|
| 366 | $time = str_replace(' ', '', $time);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 367 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 368 | // YYYYMMDDHHMMSS
|
| 369 | return mktime(
|
| 370 | substr($time, 8, 2),
|
| 371 | substr($time, 10, 2),
|
| 372 | substr($time, 12, 2),
|
| 373 | substr($time, 4, 2),
|
| 374 | substr($time, 6, 2),
|
| 375 | substr($time, 0, 4)
|
| 376 | );
|
| 377 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 378 | }
|
| 379 |
|
| 380 | // ------------------------------------------------------------------------
|
| 381 |
|
| 382 | /**
|
| 383 | * Unix to "Human"
|
| 384 | *
|
| 385 | * Formats Unix timestamp to the following prototype: 2006-08-21 11:35 PM
|
| 386 | *
|
| 387 | * @access public
|
| 388 | * @param integer Unix timestamp
|
| 389 | * @param bool whether to show seconds
|
| 390 | * @param string format: us or euro
|
| 391 | * @return string
|
| 392 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 393 | if (! function_exists('unix_to_human'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 394 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 395 | function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
|
| 396 | {
|
| 397 | $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 398 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 399 | if ($fmt == 'us')
|
| 400 | {
|
| 401 | $r .= date('h', $time).':'.date('i', $time);
|
| 402 | }
|
| 403 | else
|
| 404 | {
|
| 405 | $r .= date('H', $time).':'.date('i', $time);
|
| 406 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 407 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 408 | if ($seconds)
|
| 409 | {
|
| 410 | $r .= ':'.date('s', $time);
|
| 411 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 412 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 413 | if ($fmt == 'us')
|
| 414 | {
|
| 415 | $r .= ' '.date('A', $time);
|
| 416 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 417 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 418 | return $r;
|
| 419 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 420 | }
|
| 421 |
|
| 422 | // ------------------------------------------------------------------------
|
| 423 |
|
| 424 | /**
|
| 425 | * Convert "human" date to GMT
|
| 426 | *
|
| 427 | * Reverses the above process
|
| 428 | *
|
| 429 | * @access public
|
| 430 | * @param string format: us or euro
|
| 431 | * @return integer
|
| 432 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 433 | if (! function_exists('human_to_unix'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 434 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 435 | function human_to_unix($datestr = '')
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 436 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 437 | if ($datestr == '')
|
| 438 | {
|
| 439 | return FALSE;
|
| 440 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 441 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 442 | $datestr = trim($datestr);
|
| 443 | $datestr = preg_replace("/\040+/", "\040", $datestr);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 444 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 445 | if ( ! ereg("^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\040[0-9]{1,2}:[0-9]{1,2}.*$", $datestr))
|
| 446 | {
|
| 447 | return FALSE;
|
| 448 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 449 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 450 | $split = preg_split("/\040/", $datestr);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 451 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 452 | $ex = explode("-", $split['0']);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 453 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 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'];
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 457 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 458 | $ex = explode(":", $split['1']);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 459 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 460 | $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
|
| 461 | $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 462 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 463 | if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
|
| 464 | {
|
| 465 | $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
|
| 466 | }
|
| 467 | else
|
| 468 | {
|
| 469 | // Unless specified, seconds get set to zero.
|
| 470 | $sec = '00';
|
| 471 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 472 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 473 | if (isset($split['2']))
|
| 474 | {
|
| 475 | $ampm = strtolower($split['2']);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 476 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 477 | if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
|
| 478 | $hour = $hour + 12;
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 479 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 480 | if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
|
| 481 | $hour = '00';
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 482 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 483 | if (strlen($hour) == 1)
|
| 484 | $hour = '0'.$hour;
|
| 485 | }
|
| 486 |
|
| 487 | return mktime($hour, $min, $sec, $month, $day, $year);
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 488 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 489 | }
|
| 490 |
|
| 491 | // ------------------------------------------------------------------------
|
| 492 |
|
| 493 | /**
|
| 494 | * Timezone Menu
|
| 495 | *
|
| 496 | * Generates a drop-down menu of timezones.
|
| 497 | *
|
| 498 | * @access public
|
| 499 | * @param string timezone
|
| 500 | * @param string classname
|
| 501 | * @param string menu name
|
| 502 | * @return string
|
| 503 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 504 | if (! function_exists('timezone_menu'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 505 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 506 | function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 507 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 508 | $CI =& get_instance();
|
| 509 | $CI->lang->load('date');
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 510 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 511 | if ($default == 'GMT')
|
| 512 | $default = 'UTC';
|
| 513 |
|
| 514 | $menu = '<select name="'.$name.'"';
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 515 |
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 516 | if ($class != '')
|
| 517 | {
|
| 518 | $menu .= ' class="'.$class.'"';
|
| 519 | }
|
| 520 |
|
| 521 | $menu .= ">\n";
|
| 522 |
|
| 523 | foreach (timezones() as $key => $val)
|
| 524 | {
|
| 525 | $selected = ($default == $key) ? " selected='selected'" : '';
|
| 526 | $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
|
| 527 | }
|
| 528 |
|
| 529 | $menu .= "</select>";
|
| 530 |
|
| 531 | return $menu;
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 532 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 533 | }
|
| 534 |
|
| 535 | // ------------------------------------------------------------------------
|
| 536 |
|
| 537 | /**
|
| 538 | * Timezones
|
| 539 | *
|
| 540 | * Returns an array of timezones. This is a helper function
|
| 541 | * for various other ones in this library
|
| 542 | *
|
| 543 | * @access public
|
| 544 | * @param string timezone
|
| 545 | * @return string
|
| 546 | */
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 547 | if (! function_exists('timezones'))
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 548 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 549 | function timezones($tz = '')
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 550 | {
|
Derek Jones | 269b942 | 2008-01-28 21:00:20 +0000 | [diff] [blame] | 551 | // Note: Don't change the order of these even though
|
| 552 | // some items appear to be in the wrong order
|
| 553 |
|
| 554 | $zones = array(
|
| 555 | 'UM12' => -12,
|
| 556 | 'UM11' => -11,
|
| 557 | 'UM10' => -10,
|
| 558 | 'UM9' => -9,
|
| 559 | 'UM8' => -8,
|
| 560 | 'UM7' => -7,
|
| 561 | 'UM6' => -6,
|
| 562 | 'UM5' => -5,
|
| 563 | 'UM4' => -4,
|
| 564 | 'UM25' => -2.5,
|
| 565 | 'UM3' => -3,
|
| 566 | 'UM2' => -2,
|
| 567 | 'UM1' => -1,
|
| 568 | 'UTC' => 0,
|
| 569 | 'UP1' => +1,
|
| 570 | 'UP2' => +2,
|
| 571 | 'UP3' => +3,
|
| 572 | 'UP25' => +2.5,
|
| 573 | 'UP4' => +4,
|
| 574 | 'UP35' => +3.5,
|
| 575 | 'UP5' => +5,
|
| 576 | 'UP45' => +4.5,
|
| 577 | 'UP6' => +6,
|
| 578 | 'UP7' => +7,
|
| 579 | 'UP8' => +8,
|
| 580 | 'UP9' => +9,
|
| 581 | 'UP85' => +8.5,
|
| 582 | 'UP10' => +10,
|
| 583 | 'UP11' => +11,
|
| 584 | 'UP12' => +12
|
| 585 | );
|
| 586 |
|
| 587 | if ($tz == '')
|
| 588 | {
|
| 589 | return $zones;
|
| 590 | }
|
| 591 |
|
| 592 | if ($tz == 'GMT')
|
| 593 | $tz = 'UTC';
|
| 594 |
|
| 595 | return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 596 | }
|
Derek Allard | 8389487 | 2007-03-13 20:51:52 +0000 | [diff] [blame] | 597 | }
|
| 598 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 599 | ?> |