swapnesh
swapnesh

Reputation: 26722

DateTime conversion to support mysql datetime datetype

How to convert this date-time "17 Apr, 2012 05:50 PM" in php to support mysql datetime format ie 2012-04-17 17:50:00 (seconds as 00 by default).

Upvotes: 0

Views: 155

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

Use standard DateTime::createFromFormat class,

$date = DateTime::createFromFormat('d M, Y H:i A', '17 Apr, 2012 05:50 PM');
echo $date->format('Y-m-d H:i:s');

Update: Just did some benchmark. It seems DateTime::createFromFormat method is 1.3x faster thann strtotime, str_replace method.

Upvotes: 3

safarov
safarov

Reputation: 7804

Use strtotime() function

 $time = str_replace(',', '', "17 Apr, 2012 05:50 PM"); //remove comma
 $mysql_date =  date('Y-m-d H:i:s', strtotime($time));

Upvotes: 2

Shaun
Shaun

Reputation: 2311

Convert the time using strtotime function

Upvotes: -1

Related Questions