开发者

PHPExcel date field does not match source spreadsheet

开发者 https://www.devze.com 2023-02-25 05:26 出处:网络
This is what is in my spreadsheet: 12/04/20118:56:17 p.m. (xls dateserial = 40645.87242) this is my code I\'m using to extract the date and convert to date string in PHP:

This is what is in my spreadsheet:

12/04/2011  8:56:17 p.m. (xls dateserial = 40645.87242)

this is my code I'm using to extract the date and convert to date string in PHP:

$txn_date_xls = $sheet->getCell("H$row")->getValue();               
echo "Txn Date (original): $txn_date_xls<br />";

$txn_date = PHPExcel_Shared_Date::ExcelToPHP($txn_date_xls);
echo "Txn Date: ".date('Y-m-d H:i:s', $txn_date)."<br />";

This is the output from my script:

Txn Date (original): 40644.5578开发者_Go百科3637732
Txn Date: 2011-04-11 13:23:17

The date serial doesn't match the original spreadsheet. So, I'm trying to determine if this is a bug with PHPExcel or I have missed a setting or something, that I need to do before extracting the dateserial from the cell. Possibly a timezone issue?

Sorry, I'm not very familiar with the PHPExcel library yet.


I'm not so sure about this but...

Function date() in PHP depends of server's time zone setting.

PHPExcel_Shared_Date::ExcelToPHP() assumes that the date supplied is GMT and returns the Unix time accordingly.

So try to set GMT/UTC for PHP timezone at the beginning of script (using date_default_timezone_set())

date_default_timezone_set('UTC');

or like this (using ini_set())

ini_set('date.timezone', 'UTC');

Also, try using gmdate() instead of date(), otherwise you may get a 1 day offset.

Usable link:

  • http://phpexcel.codeplex.com/


Define

date_default_timezone_set(YOUR_TIME_ZONE);

Change this line:

date('Y-m-d H:i:s', $txn_date)

to:

 PHPExcel_Style_NumberFormat::toFormattedString($txn_date_xls, 'Y-m-d H:i:s')
0

精彩评论

暂无评论...
验证码 换一张
取 消