0

I created CSV file having date field in format 12-08-2018. When I opened csv file in Excel sheet it displays and saves in format 12/AUG/2018 to file.

When I am trying to parse it gives me wrong:

<?php
     echo date('Y-m-d',strtotime('12/AUG/2018')); // 1969-12-31
?> 

So, What could be the best way to handle parsing of date coming form excel sheet.

1 Answer 1

5

Use createFromFormat function

$date = DateTime::createFromFormat('d/M/Y', '12/AUG/2018');
echo $date->format('Y-m-d'); // 2018-08-12
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @splash58 for quick response. If I use DateTime::createFromFormat(), It means, I should be knowing what formats are expected from excel sheet. Because data coming from excel sheet can be in different formats. So, Should I be handling all cases? Isn't there a solution like automatic format detection and parsing ?
Only some fixed formats can be recognized by datetime functions. In other cases you should try all possible formats
how will we detect what is format of date coming in ? To pass it further on.
if you know the set of formats, try them while datetime fails
For e.g. After failure of datetime, one format I find 12/AUG/2018, Another format I find 12.AUG.2018, So how will I decide which format to be used ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.