ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
DateTime.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
35  require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36 }
37 
38 
47 
48  public static function _isLeapYear($year) {
49  return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
50  } // function _isLeapYear()
51 
52 
53  private static function _dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS) {
54  if ($startDay == 31) {
55  --$startDay;
56  } elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::_isLeapYear($startYear))))) {
57  $startDay = 30;
58  }
59  if ($endDay == 31) {
60  if ($methodUS && $startDay != 30) {
61  $endDay = 1;
62  if ($endMonth == 12) {
63  ++$endYear;
64  $endMonth = 1;
65  } else {
66  ++$endMonth;
67  }
68  } else {
69  $endDay = 30;
70  }
71  }
72 
73  return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
74  } // function _dateDiff360()
75 
76 
83  public static function _getDateValue($dateValue) {
84  if (!is_numeric($dateValue)) {
87  }
88  if ((is_object($dateValue)) && ($dateValue instanceof PHPExcel_Shared_Date::$dateTimeObjectType)) {
89  $dateValue = PHPExcel_Shared_Date::PHPToExcel($dateValue);
90  } else {
93  $dateValue = self::DATEVALUE($dateValue);
95  }
96  }
97  return $dateValue;
98  } // function _getDateValue()
99 
100 
107  private static function _getTimeValue($timeValue) {
110  $timeValue = self::TIMEVALUE($timeValue);
112  return $timeValue;
113  } // function _getTimeValue()
114 
115 
116  private static function _adjustDateByMonths($dateValue = 0, $adjustmentMonths = 0) {
117  // Execute function
118  $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
119  $oMonth = (int) $PHPDateObject->format('m');
120  $oYear = (int) $PHPDateObject->format('Y');
121 
122  $adjustmentMonthsString = (string) $adjustmentMonths;
123  if ($adjustmentMonths > 0) {
124  $adjustmentMonthsString = '+'.$adjustmentMonths;
125  }
126  if ($adjustmentMonths != 0) {
127  $PHPDateObject->modify($adjustmentMonthsString.' months');
128  }
129  $nMonth = (int) $PHPDateObject->format('m');
130  $nYear = (int) $PHPDateObject->format('Y');
131 
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);
137  }
138  return $PHPDateObject;
139  } // function _adjustDateByMonths()
140 
141 
148  public static function DATETIMENOW() {
149  $saveTimeZone = date_default_timezone_get();
150  date_default_timezone_set('UTC');
151  $retValue = False;
154  $retValue = (float) PHPExcel_Shared_Date::PHPToExcel(time());
155  break;
157  $retValue = (integer) time();
158  break;
160  $retValue = new DateTime();
161  break;
162  }
163  date_default_timezone_set($saveTimeZone);
164 
165  return $retValue;
166  } // function DATETIMENOW()
167 
168 
175  public static function DATENOW() {
176  $saveTimeZone = date_default_timezone_get();
177  date_default_timezone_set('UTC');
178  $retValue = False;
179  $excelDateTime = floor(PHPExcel_Shared_Date::PHPToExcel(time()));
182  $retValue = (float) $excelDateTime;
183  break;
185  $retValue = (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateTime) - 3600;
186  break;
188  $retValue = PHPExcel_Shared_Date::ExcelToPHPObject($excelDateTime);
189  break;
190  }
191  date_default_timezone_set($saveTimeZone);
192 
193  return $retValue;
194  } // function DATENOW()
195 
196 
206  public static function DATE($year = 0, $month = 1, $day = 1) {
208  $month = (integer) PHPExcel_Calculation_Functions::flattenSingleValue($month);
210 
212  // Validate parameters
213  if ($year < ($baseYear-1900)) {
215  }
216  if ((($baseYear-1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
218  }
219 
220  if (($year < $baseYear) && ($year >= ($baseYear-1900))) {
221  $year += 1900;
222  }
223 
224  if ($month < 1) {
225  // Handle year/month adjustment if month < 1
226  --$month;
227  $year += ceil($month / 12) - 1;
228  $month = 13 - abs($month % 12);
229  } elseif ($month > 12) {
230  // Handle year/month adjustment if month > 12
231  $year += floor($month / 12);
232  $month = ($month % 12);
233  }
234 
235  // Re-validate the year parameter after adjustments
236  if (($year < $baseYear) || ($year >= 10000)) {
238  }
239 
240  // Execute function
241  $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year, $month, $day);
244  return (float) $excelDateValue;
245  break;
247  return (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
248  break;
250  return PHPExcel_Shared_Date::ExcelToPHPObject($excelDateValue);
251  break;
252  }
253  } // function DATE()
254 
255 
265  public static function TIME($hour = 0, $minute = 0, $second = 0) {
269 
270  if ($hour == '') { $hour = 0; }
271  if ($minute == '') { $minute = 0; }
272  if ($second == '') { $second = 0; }
273 
274  if ((!is_numeric($hour)) || (!is_numeric($minute)) || (!is_numeric($second))) {
276  }
277  $hour = (integer) $hour;
278  $minute = (integer) $minute;
279  $second = (integer) $second;
280 
281  if ($second < 0) {
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;
288  }
289  if ($minute < 0) {
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;
296  }
297 
298  if ($hour > 23) {
299  $hour = $hour % 24;
300  } elseif ($hour < 0) {
302  }
303 
304  // Execute function
307  $date = 0;
310  $date = 1;
311  }
312  return (float) PHPExcel_Shared_Date::FormattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
313  break;
315  return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::FormattedPHPToExcel(1970, 1, 1, $hour-1, $minute, $second)); // -2147468400; // -2147472000 + 3600
316  break;
318  $dayAdjust = 0;
319  if ($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);
325  $hour = $hour % 24;
326  }
327  $phpDateObject = new DateTime('1900-01-01 '.$hour.':'.$minute.':'.$second);
328  if ($dayAdjust != 0) {
329  $phpDateObject->modify($dayAdjust.' days');
330  }
331  return $phpDateObject;
332  break;
333  }
334  } // function TIME()
335 
336 
344  public static function DATEVALUE($dateValue = 1) {
345  $dateValue = trim(PHPExcel_Calculation_Functions::flattenSingleValue($dateValue),'"');
346  // Strip any ordinals because they're allowed in Excel (English only)
347  $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui','$1$3',$dateValue);
348  // Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
349  $dateValue = str_replace(array('/','.','-',' '),array(' ',' ',' ',' '),$dateValue);
350 
351  $yearFound = false;
352  $t1 = explode(' ',$dateValue);
353  foreach($t1 as &$t) {
354  if ((is_numeric($t)) && ($t > 31)) {
355  if ($yearFound) {
357  } else {
358  if ($t < 100) { $t += 1900; }
359  $yearFound = true;
360  }
361  }
362  }
363  if ((count($t1) == 1) && (strpos($t,':') != false)) {
364  // We've been fed a time value without any date
365  return 0.0;
366  } elseif (count($t1) == 2) {
367  // We only have two parts of the date: either day/month or month/year
368  if ($yearFound) {
369  array_unshift($t1,1);
370  } else {
371  array_push($t1,date('Y'));
372  }
373  }
374  unset($t);
375  $dateValue = implode(' ',$t1);
376 
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');
386  }
387  } else {
389  }
390  } else {
392  }
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)) {
398  }
399  }
400  }
401 
402  if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
403  // Execute function
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']));
408 
411  return (float) $excelDateValue;
412  break;
414  return (integer) PHPExcel_Shared_Date::ExcelToPHP($excelDateValue);
415  break;
417  return new DateTime($PHPDateArray['year'].'-'.$PHPDateArray['month'].'-'.$PHPDateArray['day'].' 00:00:00');
418  break;
419  }
420  }
422  } // function DATEVALUE()
423 
424 
432  public static function TIMEVALUE($timeValue) {
433  $timeValue = trim(PHPExcel_Calculation_Functions::flattenSingleValue($timeValue),'"');
434  $timeValue = str_replace(array('/','.'),array('-','-'),$timeValue);
435 
436  $PHPDateArray = date_parse($timeValue);
437  if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
439  $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray['year'],$PHPDateArray['month'],$PHPDateArray['day'],$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']);
440  } else {
441  $excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel(1900,1,1,$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']) - 1;
442  }
443 
446  return (float) $excelDateValue;
447  break;
449  return (integer) $phpDateValue = PHPExcel_Shared_Date::ExcelToPHP($excelDateValue+25569) - 3600;;
450  break;
452  return new DateTime('1900-01-01 '.$PHPDateArray['hour'].':'.$PHPDateArray['minute'].':'.$PHPDateArray['second']);
453  break;
454  }
455  }
457  } // function TIMEVALUE()
458 
459 
468  public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D') {
471  $unit = strtoupper(PHPExcel_Calculation_Functions::flattenSingleValue($unit));
472 
473  if (is_string($startDate = self::_getDateValue($startDate))) {
475  }
476  if (is_string($endDate = self::_getDateValue($endDate))) {
478  }
479 
480  // Validate parameters
481  if ($startDate >= $endDate) {
483  }
484 
485  // Execute function
486  $difference = $endDate - $startDate;
487 
488  $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
489  $startDays = $PHPStartDateObject->format('j');
490  $startMonths = $PHPStartDateObject->format('n');
491  $startYears = $PHPStartDateObject->format('Y');
492 
493  $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
494  $endDays = $PHPEndDateObject->format('j');
495  $endMonths = $PHPEndDateObject->format('n');
496  $endYears = $PHPEndDateObject->format('Y');
497 
499  switch ($unit) {
500  case 'D':
501  $retVal = intval($difference);
502  break;
503  case 'M':
504  $retVal = intval($endMonths - $startMonths) + (intval($endYears - $startYears) * 12);
505  // We're only interested in full months
506  if ($endDays < $startDays) {
507  --$retVal;
508  }
509  break;
510  case 'Y':
511  $retVal = intval($endYears - $startYears);
512  // We're only interested in full months
513  if ($endMonths < $startMonths) {
514  --$retVal;
515  } elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
516  --$retVal;
517  }
518  break;
519  case 'MD':
520  if ($endDays < $startDays) {
521  $retVal = $endDays;
522  $PHPEndDateObject->modify('-'.$endDays.' days');
523  $adjustDays = $PHPEndDateObject->format('j');
524  if ($adjustDays > $startDays) {
525  $retVal += ($adjustDays - $startDays);
526  }
527  } else {
528  $retVal = $endDays - $startDays;
529  }
530  break;
531  case 'YM':
532  $retVal = intval($endMonths - $startMonths);
533  if ($retVal < 0) $retVal = 12 + $retVal;
534  // We're only interested in full months
535  if ($endDays < $startDays) {
536  --$retVal;
537  }
538  break;
539  case 'YD':
540  $retVal = intval($difference);
541  if ($endYears > $startYears) {
542  while ($endYears > $startYears) {
543  $PHPEndDateObject->modify('-1 year');
544  $endYears = $PHPEndDateObject->format('Y');
545  }
546  $retVal = $PHPEndDateObject->format('z') - $PHPStartDateObject->format('z');
547  if ($retVal < 0) { $retVal += 365; }
548  }
549  break;
550  }
551  return $retVal;
552  } // function DATEDIF()
553 
554 
563  public static function DAYS360($startDate = 0, $endDate = 0, $method = false) {
566 
567  if (is_string($startDate = self::_getDateValue($startDate))) {
569  }
570  if (is_string($endDate = self::_getDateValue($endDate))) {
572  }
573 
574  // Execute function
575  $PHPStartDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($startDate);
576  $startDay = $PHPStartDateObject->format('j');
577  $startMonth = $PHPStartDateObject->format('n');
578  $startYear = $PHPStartDateObject->format('Y');
579 
580  $PHPEndDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($endDate);
581  $endDay = $PHPEndDateObject->format('j');
582  $endMonth = $PHPEndDateObject->format('n');
583  $endYear = $PHPEndDateObject->format('Y');
584 
585  return self::_dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, !$method);
586  } // function DAYS360()
587 
588 
606  public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0) {
610 
611  if (is_string($startDate = self::_getDateValue($startDate))) {
613  }
614  if (is_string($endDate = self::_getDateValue($endDate))) {
616  }
617 
618  if (((is_numeric($method)) && (!is_string($method))) || ($method == '')) {
619  switch($method) {
620  case 0 :
621  return self::DAYS360($startDate,$endDate) / 360;
622  break;
623  case 1 :
624  $days = self::DATEDIF($startDate,$endDate);
625  $startYear = self::YEAR($startDate);
626  $endYear = self::YEAR($endDate);
627  $years = $endYear - $startYear + 1;
628  $leapDays = 0;
629  if ($years == 1) {
630  if (self::_isLeapYear($endYear)) {
631  $startMonth = self::MONTHOFYEAR($startDate);
632  $endMonth = self::MONTHOFYEAR($endDate);
633  $endDay = self::DAYOFMONTH($endDate);
634  if (($startMonth < 3) ||
635  (($endMonth * 100 + $endDay) >= (2 * 100 + 29))) {
636  $leapDays += 1;
637  }
638  }
639  } else {
640  for($year = $startYear; $year <= $endYear; ++$year) {
641  if ($year == $startYear) {
642  $startMonth = self::MONTHOFYEAR($startDate);
643  $startDay = self::DAYOFMONTH($startDate);
644  if ($startMonth < 3) {
645  $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
646  }
647  } elseif($year == $endYear) {
648  $endMonth = self::MONTHOFYEAR($endDate);
649  $endDay = self::DAYOFMONTH($endDate);
650  if (($endMonth * 100 + $endDay) >= (2 * 100 + 29)) {
651  $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
652  }
653  } else {
654  $leapDays += (self::_isLeapYear($year)) ? 1 : 0;
655  }
656  }
657  if ($years == 2) {
658  if (($leapDays == 0) && (self::_isLeapYear($startYear)) && ($days > 365)) {
659  $leapDays = 1;
660  } elseif ($days < 366) {
661  $years = 1;
662  }
663  }
664  $leapDays /= $years;
665  }
666  return $days / (365 + $leapDays);
667  break;
668  case 2 :
669  return self::DATEDIF($startDate,$endDate) / 360;
670  break;
671  case 3 :
672  return self::DATEDIF($startDate,$endDate) / 365;
673  break;
674  case 4 :
675  return self::DAYS360($startDate,$endDate,True) / 360;
676  break;
677  }
678  }
680  } // function YEARFRAC()
681 
682 
691  public static function NETWORKDAYS($startDate,$endDate) {
692  // Retrieve the mandatory start and end date that are referenced in the function definition
695  // Flush the mandatory start and end date that are referenced in the function definition, and get the optional days
696  $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
697  array_shift($dateArgs);
698  array_shift($dateArgs);
699 
700  // Validate the start and end dates
701  if (is_string($startDate = $sDate = self::_getDateValue($startDate))) {
703  }
704  $startDate = (float) floor($startDate);
705  if (is_string($endDate = $eDate = self::_getDateValue($endDate))) {
707  }
708  $endDate = (float) floor($endDate);
709 
710  if ($sDate > $eDate) {
711  $startDate = $eDate;
712  $endDate = $sDate;
713  }
714 
715  // Execute function
716  $startDoW = 6 - self::DAYOFWEEK($startDate,2);
717  if ($startDoW < 0) { $startDoW = 0; }
718  $endDoW = self::DAYOFWEEK($endDate,2);
719  if ($endDoW >= 6) { $endDoW = 0; }
720 
721  $wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
722  $partWeekDays = $endDoW + $startDoW;
723  if ($partWeekDays > 5) {
724  $partWeekDays -= 5;
725  }
726 
727  // Test any extra holiday parameters
728  $holidayCountedArray = array();
729  foreach ($dateArgs as $holidayDate) {
730  if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
732  }
733  if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
734  if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
735  --$partWeekDays;
736  $holidayCountedArray[] = $holidayDate;
737  }
738  }
739  }
740 
741  if ($sDate > $eDate) {
742  return 0 - ($wholeWeekDays + $partWeekDays);
743  }
744  return $wholeWeekDays + $partWeekDays;
745  } // function NETWORKDAYS()
746 
747 
756  public static function WORKDAY($startDate,$endDays) {
757  // Retrieve the mandatory start date and days that are referenced in the function definition
759  $endDays = (int) PHPExcel_Calculation_Functions::flattenSingleValue($endDays);
760  // Flush the mandatory start date and days that are referenced in the function definition, and get the optional days
761  $dateArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
762  array_shift($dateArgs);
763  array_shift($dateArgs);
764 
765  if ((is_string($startDate = self::_getDateValue($startDate))) || (!is_numeric($endDays))) {
767  }
768  $startDate = (float) floor($startDate);
769  // If endDays is 0, we always return startDate
770  if ($endDays == 0) { return $startDate; }
771 
772  $decrementing = ($endDays < 0) ? True : False;
773 
774  // Adjust the start date if it falls over a weekend
775 
776  $startDoW = self::DAYOFWEEK($startDate,3);
777  if (self::DAYOFWEEK($startDate,3) >= 5) {
778  $startDate += ($decrementing) ? -$startDoW + 4: 7 - $startDoW;
779  ($decrementing) ? $endDays++ : $endDays--;
780  }
781 
782  // Add endDays
783  $endDate = (float) $startDate + (intval($endDays / 5) * 7) + ($endDays % 5);
784 
785  // Adjust the calculated end date if it falls over a weekend
786  $endDoW = self::DAYOFWEEK($endDate,3);
787  if ($endDoW >= 5) {
788  $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
789  }
790 
791  // Test any extra holiday parameters
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))) {
798  }
799  if (self::DAYOFWEEK($holidayDate,3) < 5) {
800  $holidayDates[] = $holidayDate;
801  }
802  }
803  }
804  if ($decrementing) {
805  rsort($holidayDates, SORT_NUMERIC);
806  } else {
807  sort($holidayDates, SORT_NUMERIC);
808  }
809  foreach ($holidayDates as $holidayDate) {
810  if ($decrementing) {
811  if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
812  if (!in_array($holidayDate,$holidayCountedArray)) {
813  --$endDate;
814  $holidayCountedArray[] = $holidayDate;
815  }
816  }
817  } else {
818  if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
819  if (!in_array($holidayDate,$holidayCountedArray)) {
820  ++$endDate;
821  $holidayCountedArray[] = $holidayDate;
822  }
823  }
824  }
825  // Adjust the calculated end date if it falls over a weekend
826  $endDoW = self::DAYOFWEEK($endDate,3);
827  if ($endDoW >= 5) {
828  $endDate += ($decrementing) ? -$endDoW + 4: 7 - $endDoW;
829  }
830 
831  }
832  }
833 
836  return (float) $endDate;
837  break;
839  return (integer) PHPExcel_Shared_Date::ExcelToPHP($endDate);
840  break;
843  break;
844  }
845  } // function WORKDAY()
846 
847 
854  public static function DAYOFMONTH($dateValue = 1) {
856 
857  if (is_string($dateValue = self::_getDateValue($dateValue))) {
859  } elseif ($dateValue == 0.0) {
860  return 0;
861  } elseif ($dateValue < 0.0) {
863  }
864 
865  // Execute function
866  $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
867 
868  return (int) $PHPDateObject->format('j');
869  } // function DAYOFMONTH()
870 
871 
878  public static function DAYOFWEEK($dateValue = 1, $style = 1) {
881 
882  if (is_string($dateValue = self::_getDateValue($dateValue))) {
884  } elseif ($dateValue < 0.0) {
886  }
887 
888  // Execute function
889  $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
890  $DoW = $PHPDateObject->format('w');
891 
892  $firstDay = 1;
893  switch ($style) {
894  case 1: ++$DoW;
895  break;
896  case 2: if ($DoW == 0) { $DoW = 7; }
897  break;
898  case 3: if ($DoW == 0) { $DoW = 7; }
899  $firstDay = 0;
900  --$DoW;
901  break;
902  default:
903  }
905  // Test for Excel's 1900 leap year, and introduce the error as required
906  if (($PHPDateObject->format('Y') == 1900) && ($PHPDateObject->format('n') <= 2)) {
907  --$DoW;
908  if ($DoW < $firstDay) {
909  $DoW += 7;
910  }
911  }
912  }
913 
914  return (int) $DoW;
915  } // function DAYOFWEEK()
916 
917 
925  public static function WEEKOFYEAR($dateValue = 1, $method = 1) {
927  $method = floor(PHPExcel_Calculation_Functions::flattenSingleValue($method));
928 
929  if (!is_numeric($method)) {
931  } elseif (($method < 1) || ($method > 2)) {
933  }
934 
935  if (is_string($dateValue = self::_getDateValue($dateValue))) {
937  } elseif ($dateValue < 0.0) {
939  }
940 
941  // Execute function
942  $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
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;
950 
951  return (int) $weekOfYear;
952  } // function WEEKOFYEAR()
953 
954 
961  public static function MONTHOFYEAR($dateValue = 1) {
963 
964  if (is_string($dateValue = self::_getDateValue($dateValue))) {
966  } elseif ($dateValue < 0.0) {
968  }
969 
970  // Execute function
971  $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
972 
973  return (int) $PHPDateObject->format('n');
974  } // function MONTHOFYEAR()
975 
976 
983  public static function YEAR($dateValue = 1) {
985 
986  if (is_string($dateValue = self::_getDateValue($dateValue))) {
988  } elseif ($dateValue < 0.0) {
990  }
991 
992  // Execute function
993  $PHPDateObject = PHPExcel_Shared_Date::ExcelToPHPObject($dateValue);
994 
995  return (int) $PHPDateObject->format('Y');
996  } // function YEAR()
997 
998 
1005  public static function HOUROFDAY($timeValue = 0) {
1006  $timeValue = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
1007 
1008  if (!is_numeric($timeValue)) {
1010  $testVal = strtok($timeValue,'/-: ');
1011  if (strlen($testVal) < strlen($timeValue)) {
1013  }
1014  }
1015  $timeValue = self::_getTimeValue($timeValue);
1016  if (is_string($timeValue)) {
1018  }
1019  }
1020  // Execute function
1021  if ($timeValue >= 1) {
1022  $timeValue = fmod($timeValue,1);
1023  } elseif ($timeValue < 0.0) {
1025  }
1026  $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
1027 
1028  return (int) gmdate('G',$timeValue);
1029  } // function HOUROFDAY()
1030 
1031 
1038  public static function MINUTEOFHOUR($timeValue = 0) {
1039  $timeValue = $timeTester = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
1040 
1041  if (!is_numeric($timeValue)) {
1043  $testVal = strtok($timeValue,'/-: ');
1044  if (strlen($testVal) < strlen($timeValue)) {
1046  }
1047  }
1048  $timeValue = self::_getTimeValue($timeValue);
1049  if (is_string($timeValue)) {
1051  }
1052  }
1053  // Execute function
1054  if ($timeValue >= 1) {
1055  $timeValue = fmod($timeValue,1);
1056  } elseif ($timeValue < 0.0) {
1058  }
1059  $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
1060 
1061  return (int) gmdate('i',$timeValue);
1062  } // function MINUTEOFHOUR()
1063 
1064 
1071  public static function SECONDOFMINUTE($timeValue = 0) {
1072  $timeValue = PHPExcel_Calculation_Functions::flattenSingleValue($timeValue);
1073 
1074  if (!is_numeric($timeValue)) {
1076  $testVal = strtok($timeValue,'/-: ');
1077  if (strlen($testVal) < strlen($timeValue)) {
1079  }
1080  }
1081  $timeValue = self::_getTimeValue($timeValue);
1082  if (is_string($timeValue)) {
1084  }
1085  }
1086  // Execute function
1087  if ($timeValue >= 1) {
1088  $timeValue = fmod($timeValue,1);
1089  } elseif ($timeValue < 0.0) {
1091  }
1092  $timeValue = PHPExcel_Shared_Date::ExcelToPHP($timeValue);
1093 
1094  return (int) gmdate('s',$timeValue);
1095  } // function SECONDOFMINUTE()
1096 
1097 
1108  public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
1109  $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
1110  $adjustmentMonths = floor(PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths));
1111 
1112  if (!is_numeric($adjustmentMonths)) {
1114  }
1115 
1116  if (is_string($dateValue = self::_getDateValue($dateValue))) {
1118  }
1119 
1120  // Execute function
1121  $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths);
1122 
1125  return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
1126  break;
1128  return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
1129  break;
1131  return $PHPDateObject;
1132  break;
1133  }
1134  } // function EDATE()
1135 
1136 
1147  public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
1148  $dateValue = PHPExcel_Calculation_Functions::flattenSingleValue($dateValue);
1149  $adjustmentMonths = floor(PHPExcel_Calculation_Functions::flattenSingleValue($adjustmentMonths));
1150 
1151  if (!is_numeric($adjustmentMonths)) {
1153  }
1154 
1155  if (is_string($dateValue = self::_getDateValue($dateValue))) {
1157  }
1158 
1159  // Execute function
1160  $PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths+1);
1161  $adjustDays = (int) $PHPDateObject->format('d');
1162  $adjustDaysString = '-'.$adjustDays.' days';
1163  $PHPDateObject->modify($adjustDaysString);
1164 
1167  return (float) PHPExcel_Shared_Date::PHPToExcel($PHPDateObject);
1168  break;
1170  return (integer) PHPExcel_Shared_Date::ExcelToPHP(PHPExcel_Shared_Date::PHPToExcel($PHPDateObject));
1171  break;
1173  return $PHPDateObject;
1174  break;
1175  }
1176  } // function EOMONTH()
1177 
1178 } // class PHPExcel_Calculation_DateTime