JsLearner
JsLearner

Reputation: 466

PHP- Convert String to Datetime Format

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.

Upvotes: 6

Views: 13797

Answers (1)

Manwal
Manwal

Reputation: 23816

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

Upvotes: 12

Related Questions