0

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?

3
  • 1
    Have you tried using strtotime()? Commented Jun 23, 2016 at 22:10
  • 1
    Do you want to store date with timezone in database? Commented Jun 23, 2016 at 22:14
  • I've used string to time, but it gives me this: 1969-12-31 19:00. Also, I don't need timezone, I am going to fix everything in db to use one time zone (or maybe unix time) to keep things straight. Commented Jun 23, 2016 at 22:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It actually prints out as 1969-12-31 19:00 , I tried the same eariler and couldn't figure out why .. Actually, that is with the whole string (including the eastern daylight at the end) so I guess that must be the problem, I'll have to format the string to get rid of it first .
@NickCabrera Yes, you need to get red of the text first as it's not a valid format. I have done that by using preg_replace. Feel free to mark this as an answer if it solves your problem. Cheers
Thank you! It's been driving me crazy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.