php - 当输入格式为 YYYY 且值大于 1999 时,strtotime 返回当前年份

标签 php strtotime

我不确定这是不是一个错误,因为在搜索之后我找不到任何重复的体验 - 然而,这个让我感到难过。

虽然在一个旨在获取一堆自由文本记录并将它们转换为有用的日期记录的(相当痛苦的)脚本中,但我可信赖的 friend strtotime() 似乎让我失望了。

出于测试目的,我将代码归结为:

<?=date('Y', strtotime("1999"));?>

输出显示:1999

<?=date('Y', strtotime("1981"));?>

输出显示:1981

<?=date('Y', strtotime("2001"));?>

输出显示:2012

<?=date('Y', strtotime("2021"));?>

输出显示:2012

一切似乎都很好,直到输入超过“1999”——从那时起,当前年份前后的每一年都返回当年(2012 年)

非常感谢任何输入。

最佳答案

根据 PHP 的 date/time format文档:

The "Year (and just the year)" format only works if a time string has already been found -- otherwise this format is recognised as HH MM.

(页面上的倒数第二个注释)。

关于php - 当输入格式为 YYYY 且值大于 1999 时,strtotime 返回当前年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9421371/

相关文章:

php - JavaScript 和函数变量

php - 跟踪递归深度

php - 究竟是什么导致这个每六个月一个小时的时间计算错误?

php - 我如何检查日期与当前日期

php - 在 ob_start() 中使用 include()

javascript - Next Div 左右浮动两个div后显示不正确

Php:将可变天数添加到 Y-m-d 日期格式

php - 错误 : Call to a member function format() on a non-object Generating Each Day of Year

PHP 使用 strtotime() 解析带有时区+偏移量的日期

php - Laravel 安装/配置 - 未找到命令