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