|A respectable PHP Datetime parser||#1|
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?
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'.
The purpose is as hop guessed to accept a significant variety of user input.
posted date: 2008-12-18 03:58:00
|Re: A respectable PHP Datetime parser||#3|
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)
posted date: 2008-12-18 04:43:00
|Re: A respectable PHP Datetime parser||#4|
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.
posted date: 2008-12-18 04:44:00
|Re: A respectable PHP Datetime parser||#5|
i'm afraid that's what you have to accept when using php
posted date: 2008-12-18 04:45:00
|Re: A respectable PHP Datetime parser||#6|
It is not wrong, it is just different from what you expect. 'respectable' parsers follow gnu.org/software/shishi/manual/html_node/… as does strtotime
posted date: 2008-12-18 04:49:00
|Re: A respectable PHP Datetime parser||#7|
If you know in what format the time is represented in the string, you can use
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).
posted date: 2008-12-18 04:51:00
|Re: A respectable PHP Datetime parser||#8|
it's the "silently failing" part that is the problem.
posted date: 2008-12-18 05:03:00
|Re: A respectable PHP Datetime parser||#9|
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'...
posted date: 2008-12-18 05:16:00
|Re: A respectable PHP Datetime parser||#10|
It is not failing, it interprets your data differently from your expectations, but correct.
posted date: 2008-12-18 05:18:00
|Re: A respectable PHP Datetime parser||#11|
when you're done with the 'is_this_art' function will it be open soure ?-)
posted date: 2008-12-18 05:40:00
|Re: A respectable PHP Datetime parser||#12|
What would be the point? My code is always perfect straight out of the box, and (myopinion === fact) is most definitely true. :)
posted date: 2008-12-18 06:18:00
|select page: « 1 2 »|