00001 <?php
00002
00003
00004
00005
00006
00007
00008 class GyroDate {
00009 const ONE_MINUTE = 60;
00010 const ONE_HOUR = 3600;
00011 const ONE_DAY = 86400;
00012 const ONE_WEEK = 604800;
00013
00014 const ONE_MONTH = 2592000;
00015
00016 const ONE_YEAR = 31536000;
00017
00018 const MONDAY = 1;
00019 const TUESDAY = 2;
00020 const WEDNESDAY = 3;
00021 const THURSDAY = 4;
00022 const FRIDAY = 5;
00023 const SATURDAY = 6;
00024 const SUNDAY = 0;
00025
00026 const DAY_MONTH_YEAR = 'DMY';
00027 const MONTH_DAY_YEAR = 'MDY';
00028 const YEAR_MONTH_DAY = 'YMD';
00029
00030
00031
00032
00033
00034
00035 public static $non_workdays = array(self::SUNDAY, self::SATURDAY);
00036
00037
00038
00039
00040
00041 public static $holidays = array();
00042
00043
00044
00045
00046
00047 public static $local_date_order = self::MONTH_DAY_YEAR;
00048
00049
00050
00051
00052
00053
00054
00055 public static function datetime($string) {
00056 if (is_int($string)) {
00057 return $string;
00058 }
00059
00060 $time = false;
00061 if(empty($string)) {
00062
00063 $time = time();
00064 }
00065 elseif (preg_match('/^\d{14}$/', $string)) {
00066
00067 $time = mktime(
00068 substr($string, 8, 2),
00069 substr($string, 10, 2),
00070 substr($string, 12, 2),
00071 substr($string, 4, 2),
00072 substr($string, 6, 2),
00073 substr($string, 0, 4)
00074 );
00075
00076 }
00077 elseif (is_numeric($string)) {
00078
00079 $time = (int)$string;
00080 }
00081 else {
00082
00083 $time = strtotime($string);
00084 if ($time == -1 || $time === false) {
00085
00086 $time = false;
00087 }
00088 }
00089 return $time;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 public static function parse($date, $format) {
00103 $ret = false;
00104 $dt_arr = strptime($date, $format);
00105 if (is_array($dt_arr)) {
00106 $ret = mktime(
00107 $dt_arr['tm_hour'],
00108 $dt_arr['tm_min'],
00109 $dt_arr['tm_sec'],
00110 $dt_arr['tm_mon'] + 1,
00111 $dt_arr['tm_mday'],
00112 $dt_arr['tm_year'] + 1900
00113 );
00114 }
00115 return $ret;
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 public static function mysql_date($date, $includetime = true) {
00125 if ($includetime) {
00126 return date('Y-m-d H:i:s', $date);
00127 } else {
00128 return date('Y-m-d', $date);
00129 }
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 public static function mysql_time($date) {
00139 return date('H:i:s', $date);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148 public static function iso_date($date) {
00149 return date('c', $date);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 public static function rfc_date($date) {
00159 return date('r', $date);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 public static function http_date($date) {
00169 return gmdate('D, d M Y H:i:s \G\M\T', $date);
00170 }
00171
00172
00173
00174
00175
00176
00177
00178
00179 public static function local_date($date, $includetime = true) {
00180 $format = '';
00181 switch (self::$local_date_order) {
00182 case self::DAY_MONTH_YEAR:
00183 $format = ($includetime) ? 'j.n.Y, G:i:s' : 'j.n.Y';
00184 break;
00185 case self::MONTH_DAY_YEAR:
00186 $format = ($includetime) ? 'n/j/Y, G:i:s' : 'n/j/Y';
00187 break;
00188 case self::YEAR_MONTH_DAY:
00189 default:
00190 $format = ($includetime) ? 'Y-m-d, H:i:s' : 'Y-m-d';
00191 break;
00192 }
00193 return date($format, $date);
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 public static function add_months($date, $months) {
00205 if ($months === 0) {
00206 return $date;
00207 }
00208
00209 $newDate = strtotime("+$months months", $date);
00210 $arrDate = getdate($date);
00211 $arrNewDate = getdate($newDate);
00212 $monNew = $arrNewDate["mon"] + 12 * ($arrNewDate["year"] - $arrDate["year"]);
00213 $mon = $arrDate["mon"];
00214
00215 if ($monNew > $mon + $months) {
00216
00217
00218 $newDate = mktime(
00219 $arrDate["hours"],
00220 $arrDate["minutes"],
00221 $arrDate["seconds"],
00222 $mon + $months + 1,
00223 0,
00224 $arrDate["year"]
00225 );
00226 }
00227 return $newDate;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 public static function substract_months($date, $months) {
00239 if ($months === 0) {
00240 return $date;
00241 }
00242
00243 $newDate = strtotime("-$months months", $date);
00244 $arrDate = getdate($date);
00245 $arrNewDate = getdate($newDate);
00246
00247
00248
00249 $monNew = $arrNewDate["mon"] - 12 * ($arrDate["year"] - $arrNewDate["year"]);
00250 $mon = $arrDate["mon"];
00251
00252 if ($monNew < $mon - $months) {
00253
00254
00255 $newDate = mktime(
00256 $arrDate["hours"],
00257 $arrDate["minutes"],
00258 $arrDate["seconds"],
00259 $mon - $months + 1,
00260 1,
00261 $arrDate["year"]
00262 );
00263 }
00264 else if ($monNew > $mon - $months) {
00265
00266
00267 $newDate = mktime(
00268 $arrDate["hours"],
00269 $arrDate["minutes"],
00270 $arrDate["seconds"],
00271 $mon - $months + 1,
00272 0,
00273 $arrDate["year"]
00274 );
00275 }
00276 return $newDate;
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 public static function add_days($date, $days) {
00288 if ($days === 0) {
00289 return $date;
00290 }
00291
00292 $newDate = strtotime("+$days days", $date);
00293 return $newDate;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 public static function substract_days($date, $days) {
00305 if ($days === 0) {
00306 return $date;
00307 }
00308
00309 $newDate = strtotime("-$days days", $date);
00310 return $newDate;
00311 }
00312
00313
00314
00315
00316 public static function convert_to_days($timestamp) {
00317 $arr_date = getdate($timestamp);
00318
00319 $ret = ($arr_date['year'] - 2000) * 365;
00320 $ret += $arr_date['yday'];
00321
00322 return $ret;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331 public static function day($date) {
00332 return self::set_time(GyroDate::datetime($date), 0);
00333 }
00334
00335 public static function today() {
00336 return self::day(time());
00337 }
00338
00339
00340
00341
00342
00343
00344
00345 public static function month($date) {
00346 $ret = GyroDate::datetime($date);
00347 $ret = self::set_time($ret, 0);
00348 $ret = self::set_day($ret, 1);
00349 return $ret;
00350 }
00351
00352
00353
00354
00355
00356
00357
00358 public static function get_weekday($date) {
00359 $arr_date = getdate(self::datetime($date));
00360 return $arr_date['wday'];
00361 }
00362
00363
00364
00365
00366
00367
00368
00369 public static function get_week($date) {
00370 return Cast::int(date('W', self::datetime($date)));
00371 }
00372
00373
00374
00375
00376
00377
00378
00379 public static function get_day($date) {
00380 return intval(date('j', $date));
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391 public static function get_month($date) {
00392 return intval(date('n', $date));
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 public static function get_year($date) {
00402 return intval(date('Y', $date));
00403 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414 public static function is_holiday($date) {
00415 $date = self::day($date);
00416
00417 foreach(self::$holidays as $holiday) {
00418 $holiday = self::day($holiday);
00419 if ($date == $holiday) {
00420 return true;
00421 }
00422 }
00423 return false;
00424 }
00425
00426
00427
00428
00429
00430
00431
00432 public static function is_workday($date) {
00433 $date = self::day($date);
00434
00435 if (in_array(self::get_weekday($date), self::$non_workdays)) {
00436 return false;
00437 }
00438
00439 return !self::is_holiday($date);
00440 }
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 public static function set_time($date, $hour, $min = 0, $sec = 0) {
00452 $arr_date = getdate($date);
00453 return mktime($hour, $min, $sec, $arr_date['mon'], $arr_date['mday'], $arr_date['year']);
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463 public static function set_day($date, $day) {
00464 $arr_date = getdate($date);
00465 return mktime($arr_date['hours'], $arr_date['minutes'], $arr_date['seconds'], $arr_date['mon'], $day, $arr_date['year']);
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 public static function add_workdays($date, $days_to_add) {
00483 $absdays = abs($days_to_add);
00484 $sign = ($absdays == $days_to_add) ? 1 : -1;
00485 $one_day = $sign * self::ONE_DAY;
00486 while (!self::is_workday($date)) {
00487 $date += $one_day;
00488 }
00489 for ($i = 0; $i < $absdays; $i++) {
00490
00491 $date += $one_day;
00492 while (!self::is_workday($date)) {
00493 $date += $one_day;
00494 }
00495 }
00496 return $date;
00497 }
00498
00499
00500
00501
00502 public static function is_today($time) {
00503 return self::day($time) == self::day(time());
00504 }
00505
00506
00507
00508
00509 public static function is_this_month($time) {
00510 $arr_time = getdate(self::datetime($time));
00511 $arr_now = getdate(time());
00512 return ($arr_now['year'] == $arr_time['year'] && $arr_now['mon'] == $arr_time['mon']);
00513 }
00514
00515
00516
00517
00518 public static function is_this_year($time) {
00519 $arr_time = getdate(self::datetime($time));
00520 $arr_now = getdate(time());
00521 return ($arr_now['year'] == $arr_time['year']);
00522 }
00523 }
00524
00525 if (!class_exists('Date')) {
00526 class Date extends GyroDate {}
00527 }