一个值得尊敬的 PHP 日期时间解析器

发布于 2024-07-10 17:37:35 字数 509 浏览 8 评论 0原文

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

那是错误的......要么它应该失败,要么它应该正确解析。 你知道 php 中有任何强大的自然语言日期/时间解析器吗? 如何解析 php 中的自然语言日期时间?

编辑:

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

“16 Dec, 2010”要么是有效的 GNU 日期输入格式,要么不是。 在第一种情况下,它应该返回正确的答案,在第二种情况下,它应该返回错误的答案。 这就是我所说的“错误”。

编辑:

目的是像跳跃猜测的那样接受大量不同的用户输入。

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:

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

箜明 2024-07-17 17:37:35

如果您知道时间在字符串中以什么格式表示,则可以使用 strptime() 以及适当的格式字符串来解析它。 当它无法按照格式解释字符串时,它至少会报错。

该函数存在于 PHP 5.1.0 及更高版本中。

如果您想接受任意用户输入,您应该向用户提供清晰明显的反馈,以便她可以对错误解释的日期采取措施。 大多数时候,无论如何都不会出现问题,并且您永远无法捕获所有有问题的案例(想想美国与欧洲的格式)。

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

岁月蹉跎了容颜 2024-07-17 17:37:35

这没有错,您提供的数据不明确 - 存在天壤之别。

模糊的数据意味着您可以合理预期的最多只是“最佳猜测”。 您可能不同意它如何做出最佳猜测,但这并不是“错误”,只是对最有可能的情况有不同的看法。 如果不消除歧义,你就不能期望更多。

进一步的想法,主要是 hop 对 OP 的评论:

静默失败不是一种选择 - 决定何时或不静默失败受相同的规则约束,并且会因相同的歧义而抛出。

哪个示例字符串是错误的并且应该默默地失败? 你旁边的那个人呢? 他认为同样的错误吗? 如果您通过不并排比较来删除上下文会怎样?

这里唯一“错误”的是期望一个函数能够从总是存在歧义的数据中破译出确切的含义......这只是这些例子,我什至还没有得到日期:)( 2008 年 1 月 2 日?还是 1908 年 1 月 2 日?2008 年?)

对了,我要写一个名为“is_this_art”的函数了……

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'...

尤怨 2024-07-17 17:37:35

有一个名为 Chronic 的 Ruby 类,它具有处理方便的用户输入所需的灵活性:http://chronic.rubyforge。 org/

我确信您可以通过用 PHP 的 DateTime 替换 Ruby 的 Time 来将其移植到 PHP。

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.

凉世弥音 2024-07-17 17:37:35

strtotime 是您能找到的最好的函数。 我怀疑日期的任意字符串表示形式能否 100% 正确地解释,因为它至少需要一些有关所使用格式的信息。

换句话说:请定义自然语言(您刚刚在问题中使用了两个不同的版本,正如 php 解释器正确指出的那样)

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)

美人骨 2024-07-17 17:37:35

我对任何一个都不熟悉,尽管也许有人可以提供一个已经写好的。 同时,我建议在通过 strtotime 之前通过正则表达式或其他方式运行日期数据,并对其输出进行一些健全性检查,以查看返回的日期是否在可接受的范围内。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文