0

In our webapp, users can import their own data. One of the parameters in an Excel file is a date. Users will often upload different kinds of formats. One user will upload dd-mm-yy while another will upload yy-mm-dd or dd/mm/YYYY.

Now, I wrote the code below to parse some of them.

$buydate = str_replace("\\", "-", $buydate);
$buydate = str_replace("/", "-", );
$buydate = date("Y-m-d", strtotime($buydate));

However, when a user uploads dd-mm-yy my code will often use the wrong number as the year. Is there a method to parse all possible date strings to one date format?

user4992124
  • 1,574
  • 1
  • 17
  • 35
  • Possible duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) – Chetan Ameta Apr 03 '17 at 09:52
  • No there isn't a universal method: how could a computer possibly determine whether 01/04/2017 was the 4th of January or the 1st of April?.. can you, as a human being, tell me what date I mean? – Mark Baker Apr 03 '17 at 10:23
  • And a real Excel file maintains a date as a timestamp (that is universal) - the number of days since 1st January 1900 (or 1st January 1904, depending on the calendar used); not as a human formatted value... so is this an Excel file, or simply a csv file? – Mark Baker Apr 03 '17 at 10:25

0 Answers0