Nick Cabrera Nick Cabrera - 1 year ago 37
PHP Question

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
column like this
%Y-%m-%d %H:%i
. I tried using php
, 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?

Answer Source

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.