简体   繁体   中英

PHP date/strtotime converting to incorrect date

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")

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.

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM