PHP Spreadsheet_Excel_Reader always reading date as Nov 30, 1999

若如初见. 提交于 2019-12-25 18:13:18

问题


I am using Spreadsheet_Excel_Reader to read .xls files in PHP.

Everything goes fine until it comes to reading a date. If I am reading a date field, it will always return the date as Nov 30, 1999 (or variations of this date depending upon the format). I have tried setting the OutputEncoding and it's giving the same result. I tried dumping the 'example.xls' that comes with the library and that also produces the same result.

Any help on a workaround for this would be highly appreciated.


回答1:


You don't need to format your date in excel...if you have a date format in mind wrap it with double quote. e.g "13/04/1987" format("DD/MM/YYYY");

Spreadsheet_Excel_Reader will read this as a normal string with the double quote wrapper around it.

Then in your PHP make a query to remove and replace double quote with nothing.

$var = "13/04/1987";
 $removeQuote = str_replace('"','',$var);

After this you will have to replace every occurence of the forwardslash (/) with hypen(-).

$removeSlashes = str_replace('/','-',$removeQuote);

Then use the date function in PHP to format it to your suitability.

$format = date('Y-m-d', strtotime($removeSlashes));
echo $format;

And you'r done... full code goes below.

$var = "13/04/1987";
echo date('Y-m-d',strtotime(str_replace('/','-',str_replace('"','',$var))));


来源:https://stackoverflow.com/questions/11882040/php-spreadsheet-excel-reader-always-reading-date-as-nov-30-1999

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