Nick Cabrera
Nick Cabrera

Reputation: 7

How can I turn this string into a datetime?

I have a date string that looks like this:

Mon Jun 20 2016 00:00:00 GMT-0400 (Eastern Daylight Time)

and I want to insert into a mysql datetime column like this %Y-%m-%d %H:%i. I tried using php DateTime, but it gives me an error because there are two time zones. I can't change the returned string because it's the return value for a function in an api I'm using. How can I get this to the right format?

Upvotes: 0

Views: 36

Answers (1)

Osama Sayed
Osama Sayed

Reputation: 2023

If you try this:

$string = 'Mon Jun 20 2016 00:00:00 GMT-0400 (Eastern Daylight Time)';
$string =  preg_replace('/\(.*\)/', '', $string);// remove (Eastern Daylight Time)
echo date('Y-m-d H:i', strtotime($string));

Would print out:

'2016-06-20 00:00'

Hope this helps.

Upvotes: 1

Related Questions