PHPExcel date field does not match source spreadsheet

可紊 提交于 2019-12-11 01:29:45

问题


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.55783637732
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.


回答1:


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/



回答2:


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')


来源:https://stackoverflow.com/questions/5657511/phpexcel-date-field-does-not-match-source-spreadsheet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!