6

I have a string - 15/09/2015 12:00 - [Day/Month/Year] Format, I want to convert it to Year-Month-Day [mysql date time format].

I tried to use the following:

$myDateTime = $myDateTime->createFromFormat('d/m/Y H:i', '15/09/2015 12:00');
$newDateString = $myDateTime->format('Y-m-d H:i');

but it throws an internal server error.

3
  • 1
    date('Y-m-d',strtotime('15/09/2015 12:00')) use this Commented Sep 15, 2015 at 11:50
  • echo date('Y-m-d H:i',strtotime('15/09/2015 12:00')) result 1970-01-01 05:30. Commented Sep 15, 2015 at 11:52
  • @SunilPachlangia: xx/xx/xxxx format belongs to mm/dd/yyyy, thats why strtotime() will return false. Commented Sep 15, 2015 at 14:41

1 Answer 1

12

Use DateTime class to call function createFromFormat static function

$myDateTime = DateTime::createFromFormat('d/m/Y H:i', '15/09/2015 12:00');
$newDateString = $myDateTime->format('Y-m-d H:i');

Tested and giving Output:

2015-09-15 12:00
Sign up to request clarification or add additional context in comments.

3 Comments

Why should OP try this ?
Explained @Rizier123. Do you want anything more?
@Manwal nice job dude

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.