Source for file DateTime.php
Documentation is available at DateTime.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Calculation
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
/** PHPExcel root directory */
define('PHPEXCEL_ROOT', dirname(__FILE__ ) . '/../../');
require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
* PHPExcel_Calculation_DateTime
* @package PHPExcel_Calculation
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
} // function _isLeapYear()
private static function _dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS) {
} elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::_isLeapYear($startYear))))) {
if ($methodUS && $startDay != 30) {
return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
} // function _dateDiff360()
* @param string $dateValue
* @return mixed Excel date/time serial value, or string if error
$dateValue = PHPExcel_Shared_Date::PHPToExcel($dateValue);
$dateValue = self::DATEVALUE($dateValue);
} // function _getDateValue()
* @param string $timeValue
* @return mixed Excel date/time serial value, or string if error
private static function _getTimeValue($timeValue) {
$timeValue = self::TIMEVALUE($timeValue);
} // function _getTimeValue()
private static function _adjustDateByMonths($dateValue = 0, $adjustmentMonths = 0) {
$oMonth = (int) $PHPDateObject->format('m');
$oYear = (int) $PHPDateObject->format('Y');
$adjustmentMonthsString = (string) $adjustmentMonths;
if ($adjustmentMonths > 0) {
$adjustmentMonthsString = '+'. $adjustmentMonths;
if ($adjustmentMonths != 0) {
$PHPDateObject->modify($adjustmentMonthsString. ' months');
$nMonth = (int) $PHPDateObject->format('m');
$nYear = (int) $PHPDateObject->format('Y');
$monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
if ($monthDiff != $adjustmentMonths) {
$adjustDays = (int) $PHPDateObject->format('d');
$adjustDaysString = '-'. $adjustDays. ' days';
$PHPDateObject->modify($adjustDaysString);
} // function _adjustDateByMonths()
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
$retValue = (integer) time();
$retValue = new DateTime();
} // function DATETIMENOW()
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
$retValue = (float) $excelDateTime;
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
public static function DATE($year = 0, $month = 1, $day = 1) {
if ($year < ($baseYear- 1900)) {
if ((($baseYear- 1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
if (($year < $baseYear) && ($year >= ($baseYear- 1900))) {
// Handle year/month adjustment if month < 1
$year += ceil($month / 12) - 1;
$month = 13 - abs($month % 12);
// Handle year/month adjustment if month > 12
$year += floor($month / 12);
// Re-validate the year parameter after adjustments
if (($year < $baseYear) || ($year >= 10000)) {
return (float) $excelDateValue;
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
public static function TIME($hour = 0, $minute = 0, $second = 0) {
if ($hour == '') { $hour = 0; }
if ($minute == '') { $minute = 0; }
if ($second == '') { $second = 0; }
$minute = (integer) $minute;
$second = (integer) $second;
$minute += floor($second / 60);
$second = 60 - abs($second % 60);
if ($second == 60) { $second = 0; }
} elseif ($second >= 60) {
$minute += floor($second / 60);
$hour += floor($minute / 60);
$minute = 60 - abs($minute % 60);
if ($minute == 60) { $minute = 0; }
} elseif ($minute >= 60) {
$hour += floor($minute / 60);
$dayAdjust = floor($hour / 24);
$hour = 24 - abs($hour % 24);
if ($hour == 24) { $hour = 0; }
$dayAdjust = floor($hour / 24);
$phpDateObject = new DateTime('1900-01-01 '. $hour. ':'. $minute. ':'. $second);
$phpDateObject->modify($dayAdjust. ' days');
* @param string $dateValue
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
public static function DATEVALUE($dateValue = 1) {
// Strip any ordinals because they're allowed in Excel (English only)
$dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui','$1$3',$dateValue);
// Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany)
$dateValue = str_replace(array('/','.','-',' '),array(' ',' ',' ',' '),$dateValue);
if ($t < 100) { $t += 1900; }
// We've been fed a time value without any date
} elseif (count($t1) == 2) {
// We only have two parts of the date: either day/month or month/year
if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
$testVal1 = strtok($dateValue,'- ');
if ($testVal1 !== False) {
if ($testVal2 !== False) {
if ($testVal3 === False) {
$PHPDateArray = date_parse($testVal1. '-'. $testVal2. '-'. $testVal3);
if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
$PHPDateArray = date_parse($testVal2. '-'. $testVal1. '-'. $testVal3);
if (($PHPDateArray === False) || ($PHPDateArray['error_count'] > 0)) {
if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
if ($PHPDateArray['year'] == '') { $PHPDateArray['year'] = strftime('%Y'); }
if ($PHPDateArray['month'] == '') { $PHPDateArray['month'] = strftime('%m'); }
if ($PHPDateArray['day'] == '') { $PHPDateArray['day'] = strftime('%d'); }
return (float) $excelDateValue;
return new DateTime($PHPDateArray['year']. '-'. $PHPDateArray['month']. '-'. $PHPDateArray['day']. ' 00:00:00');
} // function DATEVALUE()
* @param string $timeValue
* @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
* depending on the value of the ReturnDateType flag
public static function TIMEVALUE($timeValue) {
$timeValue = str_replace(array('/','.'),array('-','-'),$timeValue);
if (($PHPDateArray !== False) && ($PHPDateArray['error_count'] == 0)) {
$excelDateValue = PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray['year'],$PHPDateArray['month'],$PHPDateArray['day'],$PHPDateArray['hour'],$PHPDateArray['minute'],$PHPDateArray['second']);
return (float) $excelDateValue;
return new DateTime('1900-01-01 '. $PHPDateArray['hour']. ':'. $PHPDateArray['minute']. ':'. $PHPDateArray['second']);
} // function TIMEVALUE()
* @param long $startDate Excel date serial value or a standard date string
* @param long $endDate Excel date serial value or a standard date string
* @return long Interval between the dates
public static function DATEDIF($startDate = 0, $endDate = 0, $unit = 'D') {
if (is_string($startDate = self::_getDateValue($startDate))) {
if (is_string($endDate = self::_getDateValue($endDate))) {
if ($startDate >= $endDate) {
$difference = $endDate - $startDate;
$startDays = $PHPStartDateObject->format('j');
$startMonths = $PHPStartDateObject->format('n');
$startYears = $PHPStartDateObject->format('Y');
$endDays = $PHPEndDateObject->format('j');
$endMonths = $PHPEndDateObject->format('n');
$endYears = $PHPEndDateObject->format('Y');
$retVal = intval($difference);
$retVal = intval($endMonths - $startMonths) + (intval($endYears - $startYears) * 12);
// We're only interested in full months
if ($endDays < $startDays) {
$retVal = intval($endYears - $startYears);
// We're only interested in full months
if ($endMonths < $startMonths) {
} elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
if ($endDays < $startDays) {
$PHPEndDateObject->modify('-'. $endDays. ' days');
$adjustDays = $PHPEndDateObject->format('j');
if ($adjustDays > $startDays) {
$retVal += ($adjustDays - $startDays);
$retVal = $endDays - $startDays;
$retVal = intval($endMonths - $startMonths);
if ($retVal < 0) $retVal = 12 + $retVal;
// We're only interested in full months
if ($endDays < $startDays) {
$retVal = intval($difference);
if ($endYears > $startYears) {
while ($endYears > $startYears) {
$PHPEndDateObject->modify('-1 year');
$endYears = $PHPEndDateObject->format('Y');
$retVal = $PHPEndDateObject->format('z') - $PHPStartDateObject->format('z');
if ($retVal < 0) { $retVal += 365; }
* @param long $startDate Excel date serial value or a standard date string
* @param long $endDate Excel date serial value or a standard date string
* @param boolean $method US or European Method
* @return long PHP date/time serial
public static function DAYS360($startDate = 0, $endDate = 0, $method = false) {
if (is_string($startDate = self::_getDateValue($startDate))) {
if (is_string($endDate = self::_getDateValue($endDate))) {
$startDay = $PHPStartDateObject->format('j');
$startMonth = $PHPStartDateObject->format('n');
$startYear = $PHPStartDateObject->format('Y');
$endDay = $PHPEndDateObject->format('j');
$endMonth = $PHPEndDateObject->format('n');
$endYear = $PHPEndDateObject->format('Y');
return self::_dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, !$method);
* Calculates the fraction of the year represented by the number of whole days between two dates (the start_date and the
* end_date). Use the YEARFRAC worksheet function to identify the proportion of a whole year's benefits or obligations
* to assign to a specific term.
* @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer) or date object, or a standard date string
* @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer) or date object, or a standard date string
* @param integer $method Method used for the calculation
* 0 or omitted US (NASD) 30/360
* @return float fraction of the year
public static function YEARFRAC($startDate = 0, $endDate = 0, $method = 0) {
if (is_string($startDate = self::_getDateValue($startDate))) {
if (is_string($endDate = self::_getDateValue($endDate))) {
return self::DAYS360($startDate,$endDate) / 360;
$days = self::DATEDIF($startDate,$endDate);
$startYear = self::YEAR($startDate);
$endYear = self::YEAR($endDate);
$years = $endYear - $startYear + 1;
if (self::_isLeapYear($endYear)) {
$startMonth = self::MONTHOFYEAR($startDate);
$endMonth = self::MONTHOFYEAR($endDate);
$endDay = self::DAYOFMONTH($endDate);
(($endMonth * 100 + $endDay) >= (2 * 100 + 29))) {
for($year = $startYear; $year <= $endYear; ++ $year) {
if ($year == $startYear) {
$startMonth = self::MONTHOFYEAR($startDate);
$startDay = self::DAYOFMONTH($startDate);
$leapDays += (self::_isLeapYear($year)) ? 1 : 0;
} elseif($year == $endYear) {
$endMonth = self::MONTHOFYEAR($endDate);
$endDay = self::DAYOFMONTH($endDate);
if (($endMonth * 100 + $endDay) >= (2 * 100 + 29)) {
$leapDays += (self::_isLeapYear($year)) ? 1 : 0;
$leapDays += (self::_isLeapYear($year)) ? 1 : 0;
if (($leapDays == 0) && (self::_isLeapYear($startYear)) && ($days > 365)) {
return $days / (365 + $leapDays);
return self::DATEDIF($startDate,$endDate) / 360;
return self::DATEDIF($startDate,$endDate) / 365;
return self::DAYS360($startDate,$endDate,True) / 360;
* @param mixed Start date
* @param array of mixed Optional Date Series
* @return long Interval between the dates
public static function NETWORKDAYS($startDate,$endDate) {
// Retrieve the mandatory start and end date that are referenced in the function definition
// Flush the mandatory start and end date that are referenced in the function definition, and get the optional days
// Validate the start and end dates
if (is_string($startDate = $sDate = self::_getDateValue($startDate))) {
$startDate = (float) floor($startDate);
if (is_string($endDate = $eDate = self::_getDateValue($endDate))) {
$endDate = (float) floor($endDate);
$startDoW = 6 - self::DAYOFWEEK($startDate,2);
if ($startDoW < 0) { $startDoW = 0; }
$endDoW = self::DAYOFWEEK($endDate,2);
if ($endDoW >= 6) { $endDoW = 0; }
$wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
$partWeekDays = $endDoW + $startDoW;
// Test any extra holiday parameters
$holidayCountedArray = array();
foreach ($dateArgs as $holidayDate) {
if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
$holidayCountedArray[] = $holidayDate;
return 0 - ($wholeWeekDays + $partWeekDays);
return $wholeWeekDays + $partWeekDays;
} // function NETWORKDAYS()
* @param mixed Start date
* @param mixed number of days for adjustment
* @param array of mixed Optional Date Series
* @return long Interval between the dates
public static function WORKDAY($startDate,$endDays) {
// Retrieve the mandatory start date and days that are referenced in the function definition
// Flush the mandatory start date and days that are referenced in the function definition, and get the optional days
$startDate = (float) floor($startDate);
// If endDays is 0, we always return startDate
if ($endDays == 0) { return $startDate; }
$decrementing = ($endDays < 0) ? True : False;
// Adjust the start date if it falls over a weekend
$startDoW = self::DAYOFWEEK($startDate,3);
if (self::DAYOFWEEK($startDate,3) >= 5) {
$startDate += ($decrementing) ? - $startDoW + 4: 7 - $startDoW;
($decrementing) ? $endDays++ : $endDays-- ;
$endDate = (float) $startDate + (intval($endDays / 5) * 7) + ($endDays % 5);
// Adjust the calculated end date if it falls over a weekend
$endDoW = self::DAYOFWEEK($endDate,3);
$endDate += ($decrementing) ? - $endDoW + 4: 7 - $endDoW;
// Test any extra holiday parameters
if (count($dateArgs) > 0) {
$holidayCountedArray = $holidayDates = array();
foreach ($dateArgs as $holidayDate) {
if ((!is_null($holidayDate)) && (trim($holidayDate) > '')) {
if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
if (self::DAYOFWEEK($holidayDate,3) < 5) {
$holidayDates[] = $holidayDate;
rsort($holidayDates, SORT_NUMERIC);
sort($holidayDates, SORT_NUMERIC);
foreach ($holidayDates as $holidayDate) {
if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
if (!in_array($holidayDate,$holidayCountedArray)) {
$holidayCountedArray[] = $holidayDate;
if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
if (!in_array($holidayDate,$holidayCountedArray)) {
$holidayCountedArray[] = $holidayDate;
// Adjust the calculated end date if it falls over a weekend
$endDoW = self::DAYOFWEEK($endDate,3);
$endDate += ($decrementing) ? - $endDoW + 4: 7 - $endDoW;
* @param long $dateValue Excel date serial value or a standard date string
public static function DAYOFMONTH($dateValue = 1) {
if (is_string($dateValue = self::_getDateValue($dateValue))) {
} elseif ($dateValue == 0.0) {
} elseif ($dateValue < 0.0) {
return (int) $PHPDateObject->format('j');
} // function DAYOFMONTH()
* @param long $dateValue Excel date serial value or a standard date string
public static function DAYOFWEEK($dateValue = 1, $style = 1) {
if (is_string($dateValue = self::_getDateValue($dateValue))) {
} elseif ($dateValue < 0.0) {
$DoW = $PHPDateObject->format('w');
case 2: if ($DoW == 0) { $DoW = 7; }
case 3: if ($DoW == 0) { $DoW = 7; }
// Test for Excel's 1900 leap year, and introduce the error as required
if (($PHPDateObject->format('Y') == 1900) && ($PHPDateObject->format('n') <= 2)) {
} // function DAYOFWEEK()
* @param long $dateValue Excel date serial value or a standard date string
* @param boolean $method Week begins on Sunday or Monday
* @return int Week Number
public static function WEEKOFYEAR($dateValue = 1, $method = 1) {
} elseif (($method < 1) || ($method > 2)) {
if (is_string($dateValue = self::_getDateValue($dateValue))) {
} elseif ($dateValue < 0.0) {
$dayOfYear = $PHPDateObject->format('z');
$dow = $PHPDateObject->format('w');
$PHPDateObject->modify('-'. $dayOfYear. ' days');
$dow = $PHPDateObject->format('w');
$daysInFirstWeek = 7 - (($dow + (2 - $method)) % 7);
$dayOfYear -= $daysInFirstWeek;
$weekOfYear = ceil($dayOfYear / 7) + 1;
return (int) $weekOfYear;
} // function WEEKOFYEAR()
* @param long $dateValue Excel date serial value or a standard date string
if (is_string($dateValue = self::_getDateValue($dateValue))) {
} elseif ($dateValue < 0.0) {
return (int) $PHPDateObject->format('n');
} // function MONTHOFYEAR()
* @param long $dateValue Excel date serial value or a standard date string
public static function YEAR($dateValue = 1) {
if (is_string($dateValue = self::_getDateValue($dateValue))) {
} elseif ($dateValue < 0.0) {
return (int) $PHPDateObject->format('Y');
* @param mixed $timeValue Excel time serial value or a standard time string
public static function HOUROFDAY($timeValue = 0) {
$testVal = strtok($timeValue,'/-: ');
$timeValue = self::_getTimeValue($timeValue);
$timeValue = fmod($timeValue,1);
} elseif ($timeValue < 0.0) {
return (int) gmdate('G',$timeValue);
} // function HOUROFDAY()
* @param long $timeValue Excel time serial value or a standard time string
$testVal = strtok($timeValue,'/-: ');
$timeValue = self::_getTimeValue($timeValue);
$timeValue = fmod($timeValue,1);
} elseif ($timeValue < 0.0) {
return (int) gmdate('i',$timeValue);
} // function MINUTEOFHOUR()
* @param long $timeValue Excel time serial value or a standard time string
$testVal = strtok($timeValue,'/-: ');
$timeValue = self::_getTimeValue($timeValue);
$timeValue = fmod($timeValue,1);
} elseif ($timeValue < 0.0) {
return (int) gmdate('s',$timeValue);
} // function SECONDOFMINUTE()
* Returns the serial number that represents the date that is the indicated number of months before or after a specified date
* (the start_date). Use EDATE to calculate maturity dates or due dates that fall on the same day of the month as the date of issue.
* @param long $dateValue Excel date serial value or a standard date string
* @param int $adjustmentMonths Number of months to adjust by
* @return long Excel date serial value
public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
if (is_string($dateValue = self::_getDateValue($dateValue))) {
$PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths);
* Returns the serial number for the last day of the month that is the indicated number of months before or after start_date.
* Use EOMONTH to calculate maturity dates or due dates that fall on the last day of the month.
* @param long $dateValue Excel date serial value or a standard date string
* @param int $adjustmentMonths Number of months to adjust by
* @return long Excel date serial value
public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
if (is_string($dateValue = self::_getDateValue($dateValue))) {
$PHPDateObject = self::_adjustDateByMonths($dateValue,$adjustmentMonths+ 1);
$adjustDays = (int) $PHPDateObject->format('d');
$adjustDaysString = '-'. $adjustDays. ' days';
$PHPDateObject->modify($adjustDaysString);
} // class PHPExcel_Calculation_DateTime
|