简体   繁体   中英

PHP Spreadsheet_Excel_Reader always reading date as Nov 30, 1999

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.

You don't need to format your date in excel...if you have a date format in mind wrap it with double quote. eg "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))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM