0

On some occasions data() is converting my dates incorrectly.

My date formats look like so: Fri Oct 25 15:00:00 EDT 2011

The date string comes from an external source, so I'm unable to change the format.

// output incorrect - Fri, 28 Oct 2011 15:00:00 -0400
date("r", strtotime("Fri Oct 25 15:00:00 EDT 2011"))

// output correct - Fri, 21 Oct 2011 15:00:00 -0400
date("r", strtotime("Fri Oct 21 15:00:00 EDT 2011"))

I can't figure out why just changing the day makes it fail to convert.

My end goal is to create a DateTime object but it suffers from the same problem.

// output - Fri Oct 28 19:00:00 EDT 2011
DateTime("Fri Oct 22 19:00:00 EDT 2011")
2
  • 4
    Just a shot in the dark -- October 21 and 28th are Fridays, but not the 22nd or the 25th. Commented Oct 21, 2011 at 22:17
  • Wow, that's embarrassing... how did I not notice that. I will have to speak with the devs of the feed. Thanks so much. Commented Oct 21, 2011 at 22:20

3 Answers 3

3

Oct 25/2011 is a tuesday. Strtotime is interpreting your date string as "what's the next friday AFTER Oct 25th", and is returning (what it thinks) is the correct answer: October 28th.

So, the GIGO rule applies. You're feeding in garbage, and wondering why you're getting garbage out.

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

5 Comments

GIGO rule is awesome. Never heard of that expression before :-)
Thanks for the explanation, as I said I can not change the feed but I will have to speak with the devs.
@Paramount: maybe you could change the devs then since they messed up your dates? :) BTW, you can just strip out the day out of the given dates and it will work just as good, since you have the day, month & year in it...
@Dvir Azulay Thanks. Stripping it out seems easiest, as I would just need to substr from the index of the first space.
@Paramount: Yep, a substr is definitely the easiest. strtotime is a magical function sometimes, but when it screws up, it screws up bigtime.
0

The value you're passing into strtotime doesn't tally. There's no Friday 25th, 2011.

http://www.timeanddate.com/calendar/monthly.html

1 Comment

@JackMurdoch Skipped October.
0

date() works fine. Problem is, in your examples with incorrect output specified date is not Friday.

You can strip out first 4 characters if it always is Fri.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.