我不确定这是不是一个错误,因为在搜索之后我找不到任何重复的体验 - 然而,这个让我感到难过。
虽然在一个旨在获取一堆自由文本记录并将它们转换为有用的日期记录的(相当痛苦的)脚本中,但我可信赖的 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/