Home » excel » Date format handling while reading from Excel in PHP

Date format handling while reading from Excel in PHP

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am reading from Excel and I am not sure what date format will be in the Excel file. I need it in the specific format of “Y-m-d H:i:s”.

I have an example which I got from Excel:

17/03/2018 00:00 and 3/1/2018 0:37

 echo date("Y-m-d H:i:s", strtotime('17/03/2018 00:00'));   
 echo date("Y-m-d H:i:s", strtotime('3/1/2018 0:37'));

The result I got is:

first one : 1970-01-01 05:30:00
second one : 2018-03-01 00:37:00
in which the first is wrong and the second is correct. I know that it is not correctly reading the month but how can I handle this?

Can anyone help me with handling any format which comes from Excel?

How to&Answers:
echo date("Y-m-d H:i:s", strtotime(str_replace('/','-',"17/3/2018 00:00"))); 

echo date("Y-m-d H:i:s", strtotime(str_replace('/','-','3/1/2018 0:37')));