Neeraj
Neeraj

Reputation: 9155

Convert date time with AM PM to 24 hour date format

How to convert 10/25/2014 14:00 PM to 2014-10-25 14:00:00 in php?

date('Y-m-d H:i:s', strtotime("10/25/2014 14:00 PM"));

returns

1970-01-01 03:00:00

Upvotes: 1

Views: 4150

Answers (3)

Kevin
Kevin

Reputation: 41885

I don't know what the origin of that date is (how it came to that), but you can use DateTime classes for this:

$raw_date = '10/25/2014 14:00 PM';
// to disregard that PM escape it in the format
$new_date = DateTime::createFromFormat('m/d/Y H:i \P\M', $raw_date);
echo $new_date->format('Y-m-d H:i:s'); // 2014-10-25 14:00:00

Upvotes: 1

Amit Kumar Sahu
Amit Kumar Sahu

Reputation: 495

10/25/2014 14:00 PM your input date format is not correct, you are using 12hr format so time will be 10/25/2014 02:00 PM

Upvotes: 1

gvgvgvijayan
gvgvgvijayan

Reputation: 2506

$date = "10/25/2014 14:00 PM"; //Here is the date 24 hours format with am/pm
$date = substr($date, 0, -2); //Removed the am/pm from date


echo date('Y-m-d H:i:s', strtotime($date)); //then convert it to mysql date format

Note: If you use this for date column means replace it data type to int and save unix time stamp which have more comfortable if year limited to 1970 to year 2038. For reason not to use more than 2038 read here

Upvotes: 1

Related Questions