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