Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP- Convert String to Datetime Format

Tags:

php

datetime

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.

like image 433
JsLearner Avatar asked Sep 15 '15 11:09

JsLearner


1 Answers

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
like image 160
Manwal Avatar answered Nov 14 '22 22:11

Manwal