ExcelToPHP - Incorrect UTC output when in DST

Topics: Developer Forum
May 4, 2011 at 3:35 PM

I have a problem concerning the ExcelToPHP method in Date.php. The excel cell has the following: 29/04/2011  15:24:00

This date is in DST time.  The function returns a timestamp that equates to 15:24:00 UTC, not 14:24:00 UTC.

In the PHPExcel code $dateValue is not less than 1 so the gmmktime cannot do its magic:

if ($dateValue >= 1) {
	$utcDays = $dateValue - $myExcelBaseDate;
	$returnValue = round($utcDays * 24 * 60 * 60);
	if (($returnValue <= PHP_INT_MAX) && ($returnValue >= -PHP_INT_MAX)) {
		$returnValue = (integer) $returnValue;
	}
} else {
	$hours = round($dateValue * 24);
	$mins = round($dateValue * 24 * 60) - round($hours * 60);
	$secs = round($dateValue * 24 * 60 * 60) - round($hours * 60 * 60) - round($mins * 60);
	$returnValue = (integer) gmmktime($hours, $mins, $secs);
}

My quick fix was to change:

if (($returnValue <= PHP_INT_MAX) && ($returnValue >= -PHP_INT_MAX)) {
	$returnValue = (integer) $returnValue;
}

to

if (($returnValue <= PHP_INT_MAX) && ($returnValue >= -PHP_INT_MAX)) {
	$returnValue = (integer) $returnValue;
	if(date('I', $returnValue) == 1) $returnValue -= 3600;
}

This returns the correct UTC time.

Andrew Taylor