PHP Spreadsheet_Excel_Reader总是读取日期为1999年11月30日 [英] PHP Spreadsheet_Excel_Reader always reading date as Nov 30, 1999
问题描述
我正在使用Spreadsheet_Excel_Reader在PHP中读取.xls文件。
I am using Spreadsheet_Excel_Reader to read .xls files in PHP.
一切正常,直到阅读日期。如果我正在阅读日期字段,它将始终将1999年11月30日的日期(或此日期的变化取决于格式)返回。我已经尝试设置OutputEncoding,并给出相同的结果。我尝试转储库附带的'example.xls',并产生相同的结果。
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.
推荐答案
你不需要在excel中格式化你的日期...如果你有一个日期格式包含它新新旗新新旗新新旗新新旗新新旗新新旗新新旗新例如13/04/1987格式(DD / MM / YYYY);
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将以正常字符串的形式将双引号包装读取。
Spreadsheet_Excel_Reader will read this as a normal string with the double quote wrapper around it.
然后在您的PHP中进行查询以清除并替换双引号。
Then in your PHP make a query to remove and replace double quote with nothing.
$var = "13/04/1987";
$removeQuote = str_replace('"','',$var);
之后这将不得不用hypen( - )替换每个发生的前缀(/)。
After this you will have to replace every occurence of the forwardslash (/) with hypen(-).
$removeSlashes = str_replace('/','-',$removeQuote);
然后使用PHP中的date函数格式化
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))));
这篇关于PHP Spreadsheet_Excel_Reader总是读取日期为1999年11月30日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!