strtotime Weirdness

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zardiw
Forum Newbie
Posts: 8
Joined: Mon Oct 29, 2007 11:29 pm

strtotime Weirdness

Post by zardiw »

Here's a real puzzler:

strtotime('10-31-2007')


Returns 12-28-2017

BUT

strtotime('10/31/2007')

or

strtotime('2007-10-31')

Works fine................It's crap like this that leads to baldness


Btw, here's a quick fix:

$WorkDate = str_replace("-", "/", $PostDate);


You guys getting DOS'd, cause it's sure slow............



z
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Really..

I get this:

Code: Select all

C:\Users\scott>php -r "$t = strtotime('10-31-2007'); echo date('n-d-Y', $t);"
12-31-1969
And nope, not geting dosd
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Weird... Probably a little bug or oversight, I think you should report it as a bug to the PHP team.
zardiw
Forum Newbie
Posts: 8
Joined: Mon Oct 29, 2007 11:29 pm

Post by zardiw »

Try it with /'s ..........anyway, point is moot I guess, cause that happens on v. 4.4.7, and it's going bye bye......What version did you run that on?...z
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Version 5.2.4

I wouldn't say it's a bug. The function is a bit iffy itself, trying to parse "about any english textual date description" into a timestamp.

It can't be right all the time, although seemingly month-day-year should be an easy enough time to recognize.

A more common format (for those outside of the US) is day-month-year, and it seems PHP recognizes that.

Code: Select all

C:\Users\scott>php -r "$t = strtotime('31-10-2007'); echo date('n-d-Y', $t);"
10-31-2007
So I believe in 10-31-2007 it's trying to parse it as day-month-year.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

That is strange.
You guys getting DOS'd, cause it's sure slow............
Nah we're just a bunch of cheap skates :) More about the slowdown
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: strtotime Weirdness

Post by Popcorn »

agreed scottayy,

(in PHP Version 5.2.4)

"strtotime('10-31-2007')" returned "false"

"1/2/2007" seems to be parsed as the rubbish US style
"1-2-2007" seems to be parsed as the rubbish UK style

we should all just suggest/encourage/force users use the nice unambiguous sorting-for-free ISO style "Y-m-d".
Post Reply