00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 if ( ! function_exists('now'))
00039 {
00040 function now()
00041 {
00042 $CI =& get_instance();
00043
00044 if (strtolower($CI->config->item('time_reference')) == 'gmt')
00045 {
00046 $now = time();
00047 $system_time = mktime(gmdate("H", $now), gmdate("i", $now), gmdate("s", $now), gmdate("m", $now), gmdate("d", $now), gmdate("Y", $now));
00048
00049 if (strlen($system_time) < 10)
00050 {
00051 $system_time = time();
00052 log_message('error', 'The Date class could not set a proper GMT timestamp so the local time() value was used.');
00053 }
00054
00055 return $system_time;
00056 }
00057 else
00058 {
00059 return time();
00060 }
00061 }
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 if ( ! function_exists('mdate'))
00084 {
00085 function mdate($datestr = '', $time = '')
00086 {
00087 if ($datestr == '')
00088 return '';
00089
00090 if ($time == '')
00091 $time = now();
00092
00093 $datestr = str_replace('%\\', '', preg_replace("/([a-z]+?){1}/i", "\\\\\\1", $datestr));
00094 return date($datestr, $time);
00095 }
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 if ( ! function_exists('standard_date'))
00111 {
00112 function standard_date($fmt = 'DATE_RFC822', $time = '')
00113 {
00114 $formats = array(
00115 'DATE_ATOM' => '%Y-%m-%dT%H:%i:%s%Q',
00116 'DATE_COOKIE' => '%l, %d-%M-%y %H:%i:%s UTC',
00117 'DATE_ISO8601' => '%Y-%m-%dT%H:%i:%s%O',
00118 'DATE_RFC822' => '%D, %d %M %y %H:%i:%s %O',
00119 'DATE_RFC850' => '%l, %d-%M-%y %H:%m:%i UTC',
00120 'DATE_RFC1036' => '%D, %d %M %y %H:%i:%s %O',
00121 'DATE_RFC1123' => '%D, %d %M %Y %H:%i:%s %O',
00122 'DATE_RSS' => '%D, %d %M %Y %H:%i:%s %O',
00123 'DATE_W3C' => '%Y-%m-%dT%H:%i:%s%Q'
00124 );
00125
00126 if ( ! isset($formats[$fmt]))
00127 {
00128 return FALSE;
00129 }
00130
00131 return mdate($formats[$fmt], $time);
00132 }
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 if ( ! function_exists('timespan'))
00149 {
00150 function timespan($seconds = 1, $time = '')
00151 {
00152 $CI =& get_instance();
00153 $CI->lang->load('date');
00154
00155 if ( ! is_numeric($seconds))
00156 {
00157 $seconds = 1;
00158 }
00159
00160 if ( ! is_numeric($time))
00161 {
00162 $time = time();
00163 }
00164
00165 if ($time <= $seconds)
00166 {
00167 $seconds = 1;
00168 }
00169 else
00170 {
00171 $seconds = $time - $seconds;
00172 }
00173
00174 $str = '';
00175 $years = floor($seconds / 31536000);
00176
00177 if ($years > 0)
00178 {
00179 $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
00180 }
00181
00182 $seconds -= $years * 31536000;
00183 $months = floor($seconds / 2628000);
00184
00185 if ($years > 0 OR $months > 0)
00186 {
00187 if ($months > 0)
00188 {
00189 $str .= $months.' '.$CI->lang->line((($months > 1) ? 'date_months' : 'date_month')).', ';
00190 }
00191
00192 $seconds -= $months * 2628000;
00193 }
00194
00195 $weeks = floor($seconds / 604800);
00196
00197 if ($years > 0 OR $months > 0 OR $weeks > 0)
00198 {
00199 if ($weeks > 0)
00200 {
00201 $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
00202 }
00203
00204 $seconds -= $weeks * 604800;
00205 }
00206
00207 $days = floor($seconds / 86400);
00208
00209 if ($months > 0 OR $weeks > 0 OR $days > 0)
00210 {
00211 if ($days > 0)
00212 {
00213 $str .= $days.' '.$CI->lang->line((($days > 1) ? 'date_days' : 'date_day')).', ';
00214 }
00215
00216 $seconds -= $days * 86400;
00217 }
00218
00219 $hours = floor($seconds / 3600);
00220
00221 if ($days > 0 OR $hours > 0)
00222 {
00223 if ($hours > 0)
00224 {
00225 $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
00226 }
00227
00228 $seconds -= $hours * 3600;
00229 }
00230
00231 $minutes = floor($seconds / 60);
00232
00233 if ($days > 0 OR $hours > 0 OR $minutes > 0)
00234 {
00235 if ($minutes > 0)
00236 {
00237 $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
00238 }
00239
00240 $seconds -= $minutes * 60;
00241 }
00242
00243 if ($str == '')
00244 {
00245 $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
00246 }
00247
00248 return substr(trim($str), 0, -1);
00249 }
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 if ( ! function_exists('days_in_month'))
00266 {
00267 function days_in_month($month = 0, $year = '')
00268 {
00269 if ($month < 1 OR $month > 12)
00270 {
00271 return 0;
00272 }
00273
00274 if ( ! is_numeric($year) OR strlen($year) != 4)
00275 {
00276 $year = date('Y');
00277 }
00278
00279 if ($month == 2)
00280 {
00281 if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
00282 {
00283 return 29;
00284 }
00285 }
00286
00287 $days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
00288 return $days_in_month[$month - 1];
00289 }
00290 }
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301 if ( ! function_exists('local_to_gmt'))
00302 {
00303 function local_to_gmt($time = '')
00304 {
00305 if ($time == '')
00306 $time = time();
00307
00308 return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
00309 }
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 if ( ! function_exists('gmt_to_local'))
00328 {
00329 function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
00330 {
00331 if ($time == '')
00332 {
00333 return now();
00334 }
00335
00336 $time += timezones($timezone) * 3600;
00337
00338 if ($dst == TRUE)
00339 {
00340 $time += 3600;
00341 }
00342
00343 return $time;
00344 }
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356 if ( ! function_exists('mysql_to_unix'))
00357 {
00358 function mysql_to_unix($time = '')
00359 {
00360
00361
00362
00363
00364 $time = str_replace('-', '', $time);
00365 $time = str_replace(':', '', $time);
00366 $time = str_replace(' ', '', $time);
00367
00368
00369 return mktime(
00370 substr($time, 8, 2),
00371 substr($time, 10, 2),
00372 substr($time, 12, 2),
00373 substr($time, 4, 2),
00374 substr($time, 6, 2),
00375 substr($time, 0, 4)
00376 );
00377 }
00378 }
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 if ( ! function_exists('unix_to_human'))
00394 {
00395 function unix_to_human($time = '', $seconds = FALSE, $fmt = 'us')
00396 {
00397 $r = date('Y', $time).'-'.date('m', $time).'-'.date('d', $time).' ';
00398
00399 if ($fmt == 'us')
00400 {
00401 $r .= date('h', $time).':'.date('i', $time);
00402 }
00403 else
00404 {
00405 $r .= date('H', $time).':'.date('i', $time);
00406 }
00407
00408 if ($seconds)
00409 {
00410 $r .= ':'.date('s', $time);
00411 }
00412
00413 if ($fmt == 'us')
00414 {
00415 $r .= ' '.date('A', $time);
00416 }
00417
00418 return $r;
00419 }
00420 }
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433 if ( ! function_exists('human_to_unix'))
00434 {
00435 function human_to_unix($datestr = '')
00436 {
00437 if ($datestr == '')
00438 {
00439 return FALSE;
00440 }
00441
00442 $datestr = trim($datestr);
00443 $datestr = preg_replace("/\040+/", "\040", $datestr);
00444
00445 if ( ! ereg("^[0-9]{2,4}\-[0-9]{1,2}\-[0-9]{1,2}\040[0-9]{1,2}:[0-9]{1,2}.*$", $datestr))
00446 {
00447 return FALSE;
00448 }
00449
00450 $split = preg_split("/\040/", $datestr);
00451
00452 $ex = explode("-", $split['0']);
00453
00454 $year = (strlen($ex['0']) == 2) ? '20'.$ex['0'] : $ex['0'];
00455 $month = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
00456 $day = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
00457
00458 $ex = explode(":", $split['1']);
00459
00460 $hour = (strlen($ex['0']) == 1) ? '0'.$ex['0'] : $ex['0'];
00461 $min = (strlen($ex['1']) == 1) ? '0'.$ex['1'] : $ex['1'];
00462
00463 if (isset($ex['2']) AND ereg("[0-9]{1,2}", $ex['2']))
00464 {
00465 $sec = (strlen($ex['2']) == 1) ? '0'.$ex['2'] : $ex['2'];
00466 }
00467 else
00468 {
00469
00470 $sec = '00';
00471 }
00472
00473 if (isset($split['2']))
00474 {
00475 $ampm = strtolower($split['2']);
00476
00477 if (substr($ampm, 0, 1) == 'p' AND $hour < 12)
00478 $hour = $hour + 12;
00479
00480 if (substr($ampm, 0, 1) == 'a' AND $hour == 12)
00481 $hour = '00';
00482
00483 if (strlen($hour) == 1)
00484 $hour = '0'.$hour;
00485 }
00486
00487 return mktime($hour, $min, $sec, $month, $day, $year);
00488 }
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 if ( ! function_exists('timezone_menu'))
00505 {
00506 function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
00507 {
00508 $CI =& get_instance();
00509 $CI->lang->load('date');
00510
00511 if ($default == 'GMT')
00512 $default = 'UTC';
00513
00514 $menu = '<select name="'.$name.'"';
00515
00516 if ($class != '')
00517 {
00518 $menu .= ' class="'.$class.'"';
00519 }
00520
00521 $menu .= ">\n";
00522
00523 foreach (timezones() as $key => $val)
00524 {
00525 $selected = ($default == $key) ? " selected='selected'" : '';
00526 $menu .= "<option value='{$key}'{$selected}>".$CI->lang->line($key)."</option>\n";
00527 }
00528
00529 $menu .= "</select>";
00530
00531 return $menu;
00532 }
00533 }
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 if ( ! function_exists('timezones'))
00548 {
00549 function timezones($tz = '')
00550 {
00551
00552
00553
00554 $zones = array(
00555 'UM12' => -12,
00556 'UM11' => -11,
00557 'UM10' => -10,
00558 'UM9' => -9,
00559 'UM8' => -8,
00560 'UM7' => -7,
00561 'UM6' => -6,
00562 'UM5' => -5,
00563 'UM4' => -4,
00564 'UM25' => -2.5,
00565 'UM3' => -3,
00566 'UM2' => -2,
00567 'UM1' => -1,
00568 'UTC' => 0,
00569 'UP1' => +1,
00570 'UP2' => +2,
00571 'UP3' => +3,
00572 'UP25' => +2.5,
00573 'UP4' => +4,
00574 'UP35' => +3.5,
00575 'UP5' => +5,
00576 'UP45' => +4.5,
00577 'UP6' => +6,
00578 'UP7' => +7,
00579 'UP8' => +8,
00580 'UP9' => +9,
00581 'UP85' => +8.5,
00582 'UP10' => +10,
00583 'UP11' => +11,
00584 'UP12' => +12
00585 );
00586
00587 if ($tz == '')
00588 {
00589 return $zones;
00590 }
00591
00592 if ($tz == 'GMT')
00593 $tz = 'UTC';
00594
00595 return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
00596 }
00597 }
00598
00599
00600
00601