30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
49 return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
53 private static function _dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS) {
54 if ($startDay == 31) {
56 } elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::_isLeapYear($startYear))))) {
60 if ($methodUS && $startDay != 30) {
62 if ($endMonth == 12) {
73 return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
84 if (!is_numeric($dateValue)) {
119 $oMonth = (int) $PHPDateObject->format(
'm');
120 $oYear = (int) $PHPDateObject->format(
'Y');
122 $adjustmentMonthsString = (string) $adjustmentMonths;
123 if ($adjustmentMonths > 0) {
124 $adjustmentMonthsString =
'+'.$adjustmentMonths;
126 if ($adjustmentMonths != 0) {
127 $PHPDateObject->modify($adjustmentMonthsString.
' months');
129 $nMonth = (int) $PHPDateObject->format(
'm');
130 $nYear = (int) $PHPDateObject->format(
'Y');
132 $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
133 if ($monthDiff != $adjustmentMonths) {
134 $adjustDays = (int) $PHPDateObject->format(
'd');
135 $adjustDaysString =
'-'.$adjustDays.
' days';
136 $PHPDateObject->modify($adjustDaysString);
138 return $PHPDateObject;
149 $saveTimeZone = date_default_timezone_get();
150 date_default_timezone_set(
'UTC');
157 $retValue = (integer) time();
160 $retValue =
new DateTime();
163 date_default_timezone_set($saveTimeZone);
176 $saveTimeZone = date_default_timezone_get();
177 date_default_timezone_set(
'UTC');
182 $retValue = (float) $excelDateTime;
191 date_default_timezone_set($saveTimeZone);
206 public static function DATE($year = 0, $month = 1, $day = 1) {
213 if ($year < ($baseYear-1900)) {
216 if ((($baseYear-1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
220 if (($year < $baseYear) && ($year >= ($baseYear-1900))) {
227 $year += ceil($month / 12) - 1;
228 $month = 13 - abs($month % 12);
229 } elseif ($month > 12) {
231 $year += floor($month / 12);
232 $month = ($month % 12);
236 if (($year < $baseYear) || ($year >= 10000)) {
244 return (
float) $excelDateValue;
265 public static function TIME($hour = 0, $minute = 0, $second = 0) {
270 if ($hour ==
'') { $hour = 0; }
271 if ($minute ==
'') { $minute = 0; }
272 if ($second ==
'') { $second = 0; }
274 if ((!is_numeric($hour)) || (!is_numeric($minute)) || (!is_numeric($second))) {
277 $hour = (integer) $hour;
278 $minute = (integer) $minute;
279 $second = (integer) $second;
282 $minute += floor($second / 60);
283 $second = 60 - abs($second % 60);
284 if ($second == 60) { $second = 0; }
285 } elseif ($second >= 60) {
286 $minute += floor($second / 60);
287 $second = $second % 60;
290 $hour += floor($minute / 60);
291 $minute = 60 - abs($minute % 60);
292 if ($minute == 60) { $minute = 0; }
293 } elseif ($minute >= 60) {
294 $hour += floor($minute / 60);
295 $minute = $minute % 60;
300 } elseif ($hour < 0) {
320 $dayAdjust = floor($hour / 24);
321 $hour = 24 - abs($hour % 24);
322 if ($hour == 24) { $hour = 0; }
323 } elseif ($hour >= 24) {
324 $dayAdjust = floor($hour / 24);
327 $phpDateObject =
new DateTime(
'1900-01-01 '.$hour.
':'.$minute.
':'.$second);
328 if ($dayAdjust != 0) {
329 $phpDateObject->modify($dayAdjust.
' days');
331 return $phpDateObject;
347 $dateValue = preg_replace(
'/(\d)(st|nd|rd|th)([ -\/])/Ui',
'$1$3',$dateValue);
349 $dateValue = str_replace(array(
'/',
'.',
'-',
' '),array(
' ',
' ',
' ',
' '),$dateValue);
352 $t1 = explode(
' ',$dateValue);
353 foreach($t1 as &
$t) {
354 if ((is_numeric($t)) && ($t > 31)) {
358 if ($t < 100) { $t += 1900; }
363 if ((count($t1) == 1) && (strpos($t,
':') !=
false)) {
366 } elseif (count($t1) == 2) {
369 array_unshift($t1,1);
371 array_push($t1,date(
'Y'));
375 $dateValue = implode(
' ',$t1);
377 $PHPDateArray = date_parse($dateValue);
378 if (($PHPDateArray === False) || ($PHPDateArray[
'error_count'] > 0)) {
379 $testVal1 = strtok($dateValue,
'- ');
380 if ($testVal1 !== False) {
381 $testVal2 = strtok(
'- ');
382 if ($testVal2 !== False) {
383 $testVal3 = strtok(
'- ');
384 if ($testVal3 === False) {
385 $testVal3 = strftime(
'%Y');
393 $PHPDateArray = date_parse($testVal1.
'-'.$testVal2.
'-'.$testVal3);
394 if (($PHPDateArray === False) || ($PHPDateArray[
'error_count'] > 0)) {
395 $PHPDateArray = date_parse($testVal2.
'-'.$testVal1.
'-'.$testVal3);
396 if (($PHPDateArray === False) || ($PHPDateArray[
'error_count'] > 0)) {
402 if (($PHPDateArray !== False) && ($PHPDateArray[
'error_count'] == 0)) {
404 if ($PHPDateArray[
'year'] ==
'') { $PHPDateArray[
'year'] = strftime(
'%Y'); }
405 if ($PHPDateArray[
'month'] ==
'') { $PHPDateArray[
'month'] = strftime(
'%m'); }
406 if ($PHPDateArray[
'day'] ==
'') { $PHPDateArray[
'day'] = strftime(
'%d'); }
407 $excelDateValue = floor(
PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray[
'year'],$PHPDateArray[
'month'],$PHPDateArray[
'day'],$PHPDateArray[
'hour'],$PHPDateArray[
'minute'],$PHPDateArray[
'second']));
411 return (
float) $excelDateValue;
417 return new DateTime($PHPDateArray[
'year'].
'-'.$PHPDateArray[
'month'].
'-'.$PHPDateArray[
'day'].
' 00:00:00');
434 $timeValue = str_replace(array(
'/',
'.'),array(
'-',
'-'),$timeValue);
436 $PHPDateArray = date_parse($timeValue);
437 if (($PHPDateArray !== False) && ($PHPDateArray[
'error_count'] == 0)) {
446 return (
float) $excelDateValue;
452 return new DateTime(
'1900-01-01 '.$PHPDateArray[
'hour'].
':'.$PHPDateArray[
'minute'].
':'.$PHPDateArray[
'second']);
468 public static function DATEDIF($startDate = 0, $endDate = 0, $unit =
'D') {
473 if (is_string($startDate = self::_getDateValue($startDate))) {
476 if (is_string($endDate = self::_getDateValue($endDate))) {
481 if ($startDate >= $endDate) {
486 $difference = $endDate - $startDate;
489 $startDays = $PHPStartDateObject->format(
'j');
490 $startMonths = $PHPStartDateObject->format(
'n');
491 $startYears = $PHPStartDateObject->format(
'Y');
494 $endDays = $PHPEndDateObject->format(
'j');
495 $endMonths = $PHPEndDateObject->format(
'n');
496 $endYears = $PHPEndDateObject->format(
'Y');
501 $retVal = intval($difference);
504 $retVal = intval($endMonths - $startMonths) + (intval($endYears - $startYears) * 12);
506 if ($endDays < $startDays) {
511 $retVal = intval($endYears - $startYears);
513 if ($endMonths < $startMonths) {
515 } elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
520 if ($endDays < $startDays) {
522 $PHPEndDateObject->modify(
'-'.$endDays.
' days');
523 $adjustDays = $PHPEndDateObject->format(
'j');
524 if ($adjustDays > $startDays) {
525 $retVal += ($adjustDays - $startDays);
528 $retVal = $endDays - $startDays;
532 $retVal = intval($endMonths - $startMonths);
533 if ($retVal < 0) $retVal = 12 + $retVal;
535 if ($endDays < $startDays) {
540 $retVal = intval($difference);
541 if ($endYears > $startYears) {
542 while ($endYears > $startYears) {
543 $PHPEndDateObject->modify(
'-1 year');
544 $endYears = $PHPEndDateObject->format(
'Y');
546 $retVal = $PHPEndDateObject->format(
'z') - $PHPStartDateObject->format(
'z');
547 if ($retVal < 0) { $retVal += 365; }
563 public static function DAYS360($startDate = 0, $endDate = 0, $method =
false) {
567 if (is_string($startDate = self::_getDateValue($startDate))) {
570 if (is_string($endDate = self::_getDateValue($endDate))) {
576 $startDay = $PHPStartDateObject->format(
'j');
577 $startMonth = $PHPStartDateObject->format(
'n');
578 $startYear = $PHPStartDateObject->format(
'Y');
581 $endDay = $PHPEndDateObject->format(
'j');
582 $endMonth = $PHPEndDateObject->format(
'n');
583 $endYear = $PHPEndDateObject->format(
'Y');
585 return self::_dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, !$method);
606 public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0) {
611 if (is_string($startDate = self::_getDateValue($startDate))) {
614 if (is_string($endDate = self::_getDateValue($endDate))) {
618 if (((is_numeric($method)) && (!is_string($method))) || ($method ==
'')) {
627 $years = $endYear - $startYear + 1;
630 if (self::_isLeapYear($endYear)) {
634 if (($startMonth < 3) ||
635 (($endMonth * 100 + $endDay) >= (2 * 100 + 29))) {
640 for($year = $startYear; $year <= $endYear; ++$year) {
641 if ($year == $startYear) {
644 if ($startMonth < 3) {
647 } elseif($year == $endYear) {
650 if (($endMonth * 100 + $endDay) >= (2 * 100 + 29)) {
660 } elseif ($days < 366) {
666 return $days / (365 + $leapDays);
697 array_shift($dateArgs);
698 array_shift($dateArgs);
701 if (is_string($startDate = $sDate = self::_getDateValue($startDate))) {
704 $startDate = (float) floor($startDate);
705 if (is_string($endDate = $eDate = self::_getDateValue($endDate))) {
708 $endDate = (float) floor($endDate);
710 if ($sDate > $eDate) {
717 if ($startDoW < 0) { $startDoW = 0; }
719 if ($endDoW >= 6) { $endDoW = 0; }
721 $wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
722 $partWeekDays = $endDoW + $startDoW;
723 if ($partWeekDays > 5) {
728 $holidayCountedArray = array();
729 foreach ($dateArgs as $holidayDate) {
730 if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
733 if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
734 if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
736 $holidayCountedArray[] = $holidayDate;
741 if ($sDate > $eDate) {
742 return 0 - ($wholeWeekDays + $partWeekDays);
744 return $wholeWeekDays + $partWeekDays;
756 public static function WORKDAY($startDate,$endDays) {
762 array_shift($dateArgs);
763 array_shift($dateArgs);
765 if ((is_string($startDate = self::_getDateValue($startDate))) || (!is_numeric($endDays))) {
768 $startDate = (float) floor($startDate);
770 if ($endDays == 0) {
return $startDate; }
772 $decrementing = ($endDays < 0) ? True : False;
777 if (self::DAYOFWEEK($startDate,3) >= 5) {
778 $startDate += ($decrementing) ? -$startDoW + 4: 7 - $startDoW;
779 ($decrementing) ? $endDays++ : $endDays--;
783 $endDate = (float) $startDate + (intval($endDays / 5) * 7) + ($endDays % 5);
788 $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
792 if (count($dateArgs) > 0) {
793 $holidayCountedArray = $holidayDates = array();
794 foreach ($dateArgs as $holidayDate) {
795 if ((!is_null($holidayDate)) && (trim($holidayDate) >
'')) {
796 if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
799 if (self::DAYOFWEEK($holidayDate,3) < 5) {
800 $holidayDates[] = $holidayDate;
805 rsort($holidayDates, SORT_NUMERIC);
807 sort($holidayDates, SORT_NUMERIC);
809 foreach ($holidayDates as $holidayDate) {
811 if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
812 if (!in_array($holidayDate,$holidayCountedArray)) {
814 $holidayCountedArray[] = $holidayDate;
818 if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
819 if (!in_array($holidayDate,$holidayCountedArray)) {
821 $holidayCountedArray[] = $holidayDate;
828 $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
836 return (
float) $endDate;
857 if (is_string($dateValue = self::_getDateValue($dateValue))) {
859 } elseif ($dateValue == 0.0) {
861 } elseif ($dateValue < 0.0) {
868 return (
int) $PHPDateObject->format(
'j');
878 public static function DAYOFWEEK($dateValue = 1, $style = 1) {
882 if (is_string($dateValue = self::_getDateValue($dateValue))) {
884 } elseif ($dateValue < 0.0) {
890 $DoW = $PHPDateObject->format(
'w');
896 case 2:
if ($DoW == 0) { $DoW = 7; }
898 case 3:
if ($DoW == 0) { $DoW = 7; }
906 if (($PHPDateObject->format(
'Y') == 1900) && ($PHPDateObject->format(
'n') <= 2)) {
908 if ($DoW < $firstDay) {
925 public static function WEEKOFYEAR($dateValue = 1, $method = 1) {
929 if (!is_numeric($method)) {
931 } elseif (($method < 1) || ($method > 2)) {
935 if (is_string($dateValue = self::_getDateValue($dateValue))) {
937 } elseif ($dateValue < 0.0) {
943 $dayOfYear = $PHPDateObject->format(
'z');
944 $dow = $PHPDateObject->format(
'w');
945 $PHPDateObject->modify(
'-'.$dayOfYear.
' days');
946 $dow = $PHPDateObject->format(
'w');
947 $daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7);
948 $dayOfYear -= $daysInFirstWeek;
949 $weekOfYear = ceil($dayOfYear / 7) + 1;
951 return (
int) $weekOfYear;
964 if (is_string($dateValue = self::_getDateValue($dateValue))) {
966 } elseif ($dateValue < 0.0) {
973 return (
int) $PHPDateObject->format(
'n');
983 public static function YEAR($dateValue = 1) {
986 if (is_string($dateValue = self::_getDateValue($dateValue))) {
988 } elseif ($dateValue < 0.0) {
995 return (
int) $PHPDateObject->format(
'Y');
1008 if (!is_numeric($timeValue)) {
1010 $testVal = strtok($timeValue,
'/-: ');
1011 if (strlen($testVal) < strlen($timeValue)) {
1016 if (is_string($timeValue)) {
1021 if ($timeValue >= 1) {
1022 $timeValue = fmod($timeValue,1);
1023 } elseif ($timeValue < 0.0) {
1028 return (
int) gmdate(
'G',$timeValue);
1041 if (!is_numeric($timeValue)) {
1043 $testVal = strtok($timeValue,
'/-: ');
1044 if (strlen($testVal) < strlen($timeValue)) {
1049 if (is_string($timeValue)) {
1054 if ($timeValue >= 1) {
1055 $timeValue = fmod($timeValue,1);
1056 } elseif ($timeValue < 0.0) {
1061 return (
int) gmdate(
'i',$timeValue);
1074 if (!is_numeric($timeValue)) {
1076 $testVal = strtok($timeValue,
'/-: ');
1077 if (strlen($testVal) < strlen($timeValue)) {
1082 if (is_string($timeValue)) {
1087 if ($timeValue >= 1) {
1088 $timeValue = fmod($timeValue,1);
1089 } elseif ($timeValue < 0.0) {
1094 return (
int) gmdate(
's',$timeValue);
1108 public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
1112 if (!is_numeric($adjustmentMonths)) {
1116 if (is_string($dateValue = self::_getDateValue($dateValue))) {
1131 return $PHPDateObject;
1147 public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
1151 if (!is_numeric($adjustmentMonths)) {
1155 if (is_string($dateValue = self::_getDateValue($dateValue))) {
1161 $adjustDays = (int) $PHPDateObject->format(
'd');
1162 $adjustDaysString =
'-'.$adjustDays.
' days';
1163 $PHPDateObject->modify($adjustDaysString);
1173 return $PHPDateObject;