diff --git a/src/PhpSpreadsheet/Shared/Date.php b/src/PhpSpreadsheet/Shared/Date.php index 2f7e8b8c..48fc6377 100644 --- a/src/PhpSpreadsheet/Shared/Date.php +++ b/src/PhpSpreadsheet/Shared/Date.php @@ -209,41 +209,57 @@ class Date ->format('U'); } - /** - * Convert a date from PHP to Excel + * Convert a date from PHP to an MS Excel serialized date/time value * - * @param mixed $dateValue PHP serialized date/time or date object - * @param boolean $adjustToTimezone Flag indicating whether $dateValue should be treated as - * a UST timestamp, or adjusted to UST - * @param string $timezone The timezone for finding the adjustment from UST - * @return mixed Excel date/time value - * or boolean FALSE on failure + * @param mixed $dateValue PHP serialized date/time or date object + * @return float|boolean Excel date/time value + * or boolean FALSE on failure */ - public static function PHPToExcel($dateValue = 0, $adjustToTimezone = false, $timezone = null) + public static function PHPToExcel($dateValue = 0) { - $saveTimeZone = date_default_timezone_get(); - date_default_timezone_set('UTC'); - - $timezoneAdjustment = ($adjustToTimezone) ? - PHPExcel_Shared_TimeZone::getTimezoneAdjustment($timezone ? $timezone : $saveTimeZone, $dateValue) : - 0; - - $retValue = false; if ((is_object($dateValue)) && ($dateValue instanceof \DateTimeInterface)) { - $dateValue->add(new \DateInterval('PT' . $timezoneAdjustment . 'S')); - $retValue = self::formattedPHPToExcel($dateValue->format('Y'), $dateValue->format('m'), $dateValue->format('d'), $dateValue->format('H'), $dateValue->format('i'), $dateValue->format('s')); + return self::DateTimeToExcel($dateValue); } elseif (is_numeric($dateValue)) { - $dateValue += $timezoneAdjustment; - $retValue = self::formattedPHPToExcel(date('Y', $dateValue), date('m', $dateValue), date('d', $dateValue), date('H', $dateValue), date('i', $dateValue), date('s', $dateValue)); + return self::TimestampToExcel($dateValue); } elseif (is_string($dateValue)) { - $retValue = self::stringToExcel($dateValue); + return self::stringToExcel($dateValue); } - date_default_timezone_set($saveTimeZone); - return $retValue; + return false; } + /** + * Convert a DateTime object to an MS Excel serialized date/time value + * + * @param \DateTimeInterface $dateValue PHP DateTime object + * @return float MS Excel serialized date/time value + */ + public static function dateTimeToExcel(\DateTimeInterface $dateValue = null) + { + return self::formattedPHPToExcel( + $dateValue->format('Y'), + $dateValue->format('m'), + $dateValue->format('d'), + $dateValue->format('H'), + $dateValue->format('i'), + $dateValue->format('s') + ); + } + + /** + * Convert a Unix timestamp to an MS Excel serialized date/time value + * + * @param \DateTimeInterface $dateValue PHP DateTime object + * @return float MS Excel serialized date/time value + */ + public static function timestampToExcel($dateValue = 0) + { + if (!is_numeric($dateValue)) { + return false; + } + return self::DateTimeToExcel(new \DateTime('@' . $dateValue)); + } /** * formattedPHPToExcel diff --git a/tests/PhpSpreadsheet/Shared/DateTest.php b/tests/PhpSpreadsheet/Shared/DateTest.php index 1b82e44b..eb86cf24 100644 --- a/tests/PhpSpreadsheet/Shared/DateTest.php +++ b/tests/PhpSpreadsheet/Shared/DateTest.php @@ -48,9 +48,9 @@ class DateTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider providerDateTimePHPToExcel1900 + * @dataProvider providerDateTimeTimestampToExcel1900 */ - public function testDateTimePHPToExcel1900() + public function testDateTimeTimestampToExcel1900() { $result = call_user_func( array(Date::class,'setExcelCalendar'), @@ -59,13 +59,34 @@ class DateTest extends \PHPUnit_Framework_TestCase $args = func_get_args(); $expectedResult = array_pop($args); - $result = call_user_func_array(array(Date::class,'PHPToExcel'), $args); + $result = call_user_func_array(array(Date::class,'timestampToExcel'), $args); $this->assertEquals($expectedResult, $result, null, 1E-5); } - public function providerDateTimePHPToExcel1900() + public function providerDateTimeTimestampToExcel1900() { - return include 'rawTestData/Shared/Date/PHPToExcel1900.php'; + return include 'rawTestData/Shared/Date/TimestampToExcel1900.php'; + } + + /** + * @dataProvider providerDateTimeDateTimeToExcel + */ + public function testDateTimeDateTimeToExcel() + { + $result = call_user_func( + array(Date::class,'setExcelCalendar'), + Date::CALENDAR_WINDOWS_1900 + ); + + $args = func_get_args(); + $expectedResult = array_pop($args); + $result = call_user_func_array(array(Date::class,'dateTimeToExcel'), $args); + $this->assertEquals($expectedResult, $result, null, 1E-5); + } + + public function providerDateTimeDateTimeToExcel() + { + return include 'rawTestData/Shared/Date/DateTimeToExcel.php'; } /** @@ -111,9 +132,9 @@ class DateTest extends \PHPUnit_Framework_TestCase } /** - * @dataProvider providerDateTimePHPToExcel1904 + * @dataProvider providerDateTimeTimestampToExcel1904 */ - public function testDateTimePHPToExcel1904() + public function testDateTimeTimestampToExcel1904() { $result = call_user_func( array(Date::class,'setExcelCalendar'), @@ -122,13 +143,13 @@ class DateTest extends \PHPUnit_Framework_TestCase $args = func_get_args(); $expectedResult = array_pop($args); - $result = call_user_func_array(array(Date::class,'PHPToExcel'), $args); + $result = call_user_func_array(array(Date::class,'timestampToExcel'), $args); $this->assertEquals($expectedResult, $result, null, 1E-5); } - public function providerDateTimePHPToExcel1904() + public function providerDateTimeTimestampToExcel1904() { - return include 'rawTestData/Shared/Date/PHPToExcel1904.php'; + return include 'rawTestData/Shared/Date/TimestampToExcel1904.php'; } /** diff --git a/tests/rawTestData/Shared/Date/DateTimeToExcel.php b/tests/rawTestData/Shared/Date/DateTimeToExcel.php new file mode 100644 index 00000000..c9e0e7bf --- /dev/null +++ b/tests/rawTestData/Shared/Date/DateTimeToExcel.php @@ -0,0 +1,20 @@ + 19-Dec-1960 05:00:00 UTC - [ 25569, 'America/New_York', 18000 ], // 01-Jan-1970 00:00:00 EST => 01-Jan-1970 05:00:00 UTC PHP Base Date - [ 30292, 'America/New_York', 408085200 ], // 07-Dec-1982 00:00:00 EST => 07-Dec-1982 05:00:00 UTC - [ 39611, 'America/New_York', 1213243200 ], // 12-Jun-2008 00:00:00 EDT => 12-Jun-2008 04:00:00 UTC - [ 50424, 'America/New_York', 2147490000 ], // 19-Jan-2038 00:00:00 EST => 19-Jan-2038 05:00:00 UTC PHP 32-bit Latest Date - [ 22345.56789, 'America/New_York', -278486534 ], // 05-Mar-1961 13:37:46 EST => 05-Mar-1961 18:37:46 UTC - [ 22345.6789, 'America/New_York', -278476943 ], // 05-Mar-1961 16:17:37 EST => 05-Mar-1961 21:17:37 UTC - [ 0.5, 'America/New_York', 61200 ], // 12:00:00 EST => 17:00:00 UTC - [ 0.75, 'America/New_York', 82800 ], // 18:00.00 EST => 23:00:00 UTC - [ 0.12345, 'America/New_York', 28666 ], // 02:57:46 EST => 07:57:46 UTC - [ 41215, 'America/New_York', 1351828800 ], // 02-Nov-2012 00:00:00 EDT => 02-Nov-2012 04:00:00 UTC - [ 22269, 'Pacific/Auckland', -285163200 ], // 19-Dec-1960 00:00:00 NZST => 18-Dec-1960 12:00:00 UTC - [ 25569, 'Pacific/Auckland', -43200 ], // 01-Jan-1970 00:00:00 NZST => 31-Dec-1969 12:00:00 UTC PHP Base Date - [ 30292, 'Pacific/Auckland', 408020400 ], // 07-Dec-1982 00:00:00 NZDT => 06-Dec-1982 11:00:00 UTC - [ 39611, 'Pacific/Auckland', 1213185600 ], // 12-Jun-2008 00:00:00 NZST => 11-Jun-2008 12:00:00 UTC - [ 50423.5, 'Pacific/Auckland', 2147382000 ], // 18-Jan-2038 12:00:00 NZDT => 17-Jan-2038 23:00:00 UTC PHP 32-bit Latest Date - [ 22345.56789, 'Pacific/Auckland', -278547734 ], // 05-Mar-1961 13:37:46 NZST => 05-Mar-1961 01:37:46 UTC - [ 22345.6789, 'Pacific/Auckland', -278538143 ], // 05-Mar-1961 16:17:37 NZST => 05-Mar-1961 04:17:37 UTC - [ 0.5, 'Pacific/Auckland', 0 ], // 12:00:00 NZST => 00:00:00 UTC - [ 0.75, 'Pacific/Auckland', 21600 ], // 18:00.00 NZST => 06:00:00 UTC - [ 0.12345, 'Pacific/Auckland', -32534 ], // 02:57:46 NZST => 14:57:46 UTC - [ 41215, 'Pacific/Auckland', 1351767600 ], // 02-Nov-2012 00:00:00 NZDT => 01-Nov-2012 11:00:00 UTC + [ 22269, 'America/New_York', -285102000 ], // 19-Dec-1960 00:00:00 EST => 19-Dec-1960 05:00:00 UTC + [ 25569, 'America/New_York', 18000 ], // 01-Jan-1970 00:00:00 EST => 01-Jan-1970 05:00:00 UTC PHP Base Date + [ 30292, 'America/New_York', 408085200 ], // 07-Dec-1982 00:00:00 EST => 07-Dec-1982 05:00:00 UTC + [ 39611, 'America/New_York', 1213243200 ], // 12-Jun-2008 00:00:00 EDT => 12-Jun-2008 04:00:00 UTC + [ 50424, 'America/New_York', 2147490000 ], // 19-Jan-2038 00:00:00 EST => 19-Jan-2038 05:00:00 UTC PHP 32-bit Latest Date + [ 22345.56789, 'America/New_York', -278486534 ], // 05-Mar-1961 13:37:46 EST => 05-Mar-1961 18:37:46 UTC + [ 22345.6789, 'America/New_York', -278476943 ], // 05-Mar-1961 16:17:37 EST => 05-Mar-1961 21:17:37 UTC + [ 0.5, 'America/New_York', 61200 ], // 12:00:00 EST => 17:00:00 UTC + [ 0.75, 'America/New_York', 82800 ], // 18:00.00 EST => 23:00:00 UTC + [ 0.12345, 'America/New_York', 28666 ], // 02:57:46 EST => 07:57:46 UTC + [ 41215, 'America/New_York', 1351828800 ], // 02-Nov-2012 00:00:00 EDT => 02-Nov-2012 04:00:00 UTC + [ 22269, 'Pacific/Auckland', -285163200 ], // 19-Dec-1960 00:00:00 NZST => 18-Dec-1960 12:00:00 UTC + [ 25569, 'Pacific/Auckland', -43200 ], // 01-Jan-1970 00:00:00 NZST => 31-Dec-1969 12:00:00 UTC PHP Base Date + [ 30292, 'Pacific/Auckland', 408020400 ], // 07-Dec-1982 00:00:00 NZDT => 06-Dec-1982 11:00:00 UTC + [ 39611, 'Pacific/Auckland', 1213185600 ], // 12-Jun-2008 00:00:00 NZST => 11-Jun-2008 12:00:00 UTC + [ 50423.5, 'Pacific/Auckland', 2147382000 ], // 18-Jan-2038 12:00:00 NZDT => 17-Jan-2038 23:00:00 UTC PHP 32-bit Latest Date + [ 22345.56789, 'Pacific/Auckland', -278547734 ], // 05-Mar-1961 13:37:46 NZST => 05-Mar-1961 01:37:46 UTC + [ 22345.6789, 'Pacific/Auckland', -278538143 ], // 05-Mar-1961 16:17:37 NZST => 05-Mar-1961 04:17:37 UTC + [ 0.5, 'Pacific/Auckland', 0 ], // 12:00:00 NZST => 00:00:00 UTC + [ 0.75, 'Pacific/Auckland', 21600 ], // 18:00.00 NZST => 06:00:00 UTC + [ 0.12345, 'Pacific/Auckland', -32534 ], // 02:57:46 NZST => 14:57:46 UTC + [ 41215, 'Pacific/Auckland', 1351767600 ], // 02-Nov-2012 00:00:00 NZDT => 01-Nov-2012 11:00:00 UTC ]; diff --git a/tests/rawTestData/Shared/Date/ExcelToTimestamp1904.php b/tests/rawTestData/Shared/Date/ExcelToTimestamp1904.php index b372e180..7716e66b 100644 --- a/tests/rawTestData/Shared/Date/ExcelToTimestamp1904.php +++ b/tests/rawTestData/Shared/Date/ExcelToTimestamp1904.php @@ -1,14 +1,14 @@