views:

776

answers:

5

.

echo date('r',strtotime("16 Dec, 2010")); //Tue, 16 Dec 2008 20:10:00 +0530
echo date('r',strtotime("16 Dec  2010")); //Sat, 16 Jan 2010 00:00:00 +0530

That's just wrong... Either it should fail or it should parse correctly. Do you know any robust natural language date/time parser in php? How do you parse natural language date time in php?

Edit 0:

var_dump(strtotime("16 Dec, abcd")); //bool(false)

"16 Dec, 2010" is either a valid GNU date input format or it is not. In the first case it should return the correct answer and in the second it should return false. This is what I mean by 'wrong'.

Edit 1:

The purpose is as hop guessed to accept a significant variety of user input.

A: 

strtotime is the best function you could find for that. I doubt that an arbitrary string representation of a date will ever be interpreted 100% correctly, since it would require at least some information on the formatting used.

In other words: Please define natural language (You just used two different versions thereof in your question, as the php interpreter pointed out correctly)

soulmerge
Try ruby's Date.parse . No one wants perfection here. So long as the function does what it promises to. Ruby's Date.parse does the right thing in all three cases.
anshul
A: 

I'm not familiar with any, though maybe someone can offer an already-written one. In the meantime, I'd recommend running your date data through a regex or other munging before putting it through strtotime, and using a little sanity-checking on its output to see if the returned date falls in the accepted range.

J Cooper
+2  A: 

If you know in what format the time is represented in the string, you can use strptime() together with the appropriate format string to parse it. It will at least report an error when it cannot interpret the string according to the format.

This function exists in PHP 5.1.0 and up.

If you want to take arbitrary user input, you should offer clear and obvious feedback to the user, so that she can do something about a falsely interpreted date. Most of the time, there won't be a problem anyway and you can't ever catch all problematic cases (think American vs. European format).

hop
Arbitrary user input is the idea. I do give clear feedback and auto-correct for european-american and now also the comma, but where does this chase end? I guess I will roll out my own parser when I get some leisure.
anshul
+2  A: 

It's not wrong, the data you're supplying is ambiguous - there is a world of difference.

Ambiguous data means the most you can reasonably expect from it is a 'best guess'. You might disagree with how it makes this best guess, but that's not 'wrong', that's just a different opinion on what is most likely. You can't expect any more than that without removing the ambiguity.

Further thoughts, mostly to hop's comments on the OP:

Silently failing is not an option - deciding when or not to silently fail is subject to the same rules, and will be thrown by the same ambiguities.

Which of the example strings is wrong and should silently fail? What about the guy next to you? Does he think the same one is wrong? What if you remove the context by not comparing them side by side?

The only thing 'wrong' here is expecting a function to be able to decipher an exact meaning from data that will always be subject to ambiguity... and this is just those examples, I haven't even got to dates yet :) (1/2/08 is the first of Feb? or the 2nd of Jan? 1908? 2008? 8?)

Right, that said, I'm off to write a function called 'is_this_art'...

JoeBloggs
when you're done with the 'is_this_art' function will it be open soure ?-)
Jacco
What would be the point? My code is always perfect straight out of the box, and (myopinion === fact) is most definitely true. :)
JoeBloggs
+1. Saying strtotime() is wrong is shooting the messenger.
jTresidder
nobody is saying that strtotime() should be perfect. what i'm trying to tell you is that PHP will get your hopes high and then crush you like a bug. try this: date('r', strtotime("2005-02-30")). and then tell mit that that date is ambiguous. i dare you.
hop
+1 @hop... It is the unclear partial adherence to GNU Date input formats that hurts. I really wouldn't be complaining if strtotime claimed to do a fuzzy match. It's present behaviour is inconsistent.
anshul
@hop: That's exactly what you're saying, even more so with 'hopes high'. What hopes, if not delegating the resolution of ambiguity? And how exactly is demonstrating a bug in a function that you expect to be capable of the impossible, any kind of help to your position?
jTresidder
the "hopes" of being able to use something that promises to adhere to the mentioned GNU Date input format rules and then just not doing it.
hop
+1  A: 

There is a Ruby class called Chronic that has the flexibility you need to handle convenient user input: http://chronic.rubyforge.org/

I'm sure you could just port it to PHP by replacing Ruby's Time with PHP's DateTime.

erjiang