0

I have gone through convert php date to mysql format but still I have few issues and questions,

In general, my application, can input dates in Y-m-d, d-m-y, m-d-Y, jS, F Y format, or even with '/' separator.

I want this dates to be converted in to MYSQL Y-m-d format, I tried as below, but it shows different output than expected,

Non working

date_format(date_create_from_format('d-m-Y', '02-25-2016'), 'Y-m-d');

Working

date_format(date_create_from_format('d-m-Y', '25-02-2016'), 'Y-m-d');

So it seems format and string to be match in same way, otherwise its not interpreting correctly,

What is best way to convert above 4 format inputs to mysql(Y-m-d) format?

Thanks advanced,

9
  • 2
    Why on earth do you expect date_create_from_format('d-m-Y', '02-25-2016') to work? How many years have 25 months? Of course the date value and the format mask have to match..... that's the whole point of date_create_from_format()!!! Commented May 26, 2016 at 8:28
  • @MarkBaker, i know that won't work, that's reason i asked what way to get it work regardless of input date format :-) Commented May 26, 2016 at 8:30
  • Have you tried using $date = new \DateTime($date_str); echo $date->format('Y-m-d'); Commented May 26, 2016 at 8:32
  • Can you add your output please. Commented May 26, 2016 at 8:32
  • 1
    There cannot be one all singing all dancing date format convertor, because of dates like 01-02-2016.... is that 1st February, or 2nd January? PHP will make a decision for you based on / or - separator, but there's no guarantee that it will be correct..... you need to control the date formats that you use so that you can eliminate ambiguities.... the whole point of the date_create_from_format() function is for you to tell PHP what format the date is, so that is parser can make the right decision Commented May 26, 2016 at 8:34

3 Answers 3

0

I'd recommend using Carbon for any date processing in PHP these days.

Creating date by parsing strings is simple enough...
http://carbon.nesbot.com/docs/#api-instantiation

Getting the output in a format you need is also simple...
http://carbon.nesbot.com/docs/#api-formatting

Sign up to request clarification or add additional context in comments.

Comments

0

I'd suggest you to use this function: DateTime::createFromFormat

Using this, you need to create a date by specifying the particular format.

Try this:

 $date = DateTime::createFromFormat('Y-m-d', '2016-05-26');
 echo $date->format('Y-m-d');
 echo "<hr/>";

 $date = DateTime::createFromFormat('d-m-Y', '26-05-2016');
 echo $date->format('Y-m-d');
 echo "<hr/>";

 $date = DateTime::createFromFormat('m-d-Y', '05-26-2016');
 echo $date->format('Y-m-d');
 echo "<hr/>";

 $date = DateTime::createFromFormat('jS F Y', '26th May 2016');
 echo $date->format('Y-m-d');

Comments

0

Thanks for all suggestions, i think i found my logic, My date format selection is stored in sql db, so will try as below,

date_format(date_create_from_format('format from db', 'user entered format'), 'Y-m-d');

As user entered format is stored in db, so i will pull that in this date_format function, so both will match and i can convert to Y-m-d!

Thanks,

Comments

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.