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?
strtotime('03/17/2018 00:00'). Or useDateTime::createFromFormat(). Also note that you can not convert any format to adate. format of the date should be already known. You can definitely usestrtotimeto convert almost every type of string to date but not any format01-02-03, will you interpret as 1st Feb 2003 or 2nd Jan 2003 or 3rd Jan 2002? It is not possible to come to a conclusion. So the best you can do is try to convert string to timestamp usingstrtotimeand if it fails to return a timestamp then let human handle it.