strtotime() returns 当前日期 DD.MM.YY 输入 - 为什么?

strtotime() returns current date with DD.MM.YY input - why?

假设我得到了这个代码:

echo "12.07.2016 - ".date("d.m.Y", strtotime("12.07.2016"));
echo "12.07.16 - ".date("d.m.Y", strtotime("12.07.16"));

哪个returns这个:

12.07.2016 - 12.07.2016
12.07.16 - 01.02.2016

第一个日期格式正确,第二行 returns 是当前日期 - 为什么会这样? strtotime() 是否无法处理两位数格式的年份?

编辑:澄清一下,我仍然希望 date() 以 4 位数字显示年份。似乎 strtotime() 不接受任何 2 位数年份的输入,而是 returns 当前日期的时间戳...

y 是小 return 年份的 2 位数,因此您的代码可以是:

echo "12.07.2016 - ".date("d.m.Y", strtotime("12.07.2016"));
echo "12.07.16 - ".date("d.m.y", strtotime("12.07.16"));

原因很简单,您需要在以下语句中取小 "y":

echo "12.07.16 - ".date("d.m.Y", strtotime("12.07.16"));

echo "12.07.16 - ".date("d.m.y", strtotime("12.07.16"));

您正在使用 "Y",用于 YYYY 甲酸。

你应该试试这个代码

echo "12.07.2016 - ".date("d.m.y", strtotime("12.07.2016"));// for YY format
echo "12.07.16 - ".date("d.m.Y", strtotime("12.07.16")); // for YYYY formate
echo $a = date("d-m-Y H:i:s",time()); // for current time stamp

输出:

12.07.2016 - 12.07.16
12.07.16 - 01.02.2016
01-02-2016 12:16:12

如果书写正确,它接受 2 位数字的年份。

示例:

echo $one = "12.07.2016 - ".date("d.m.Y", strtotime("12.07.2016"));
echo "</br>";
echo $two = "12.07.16 - ".date("d.m.Y", strtotime("16-07-12"));

string date ( string $format [, int $timestamp = time() ] )

Timestamp :- The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given.

In other words, it defaults to the value of time().

所以在你的情况下,在 echo "12.07.16 - ".date("d.m.Y", strtotime("12.07.16"));

您应该输入的格式是 12.07.2016 但是您输入的 12.07.16 被丢弃了所以它采用了 默认本地日期

You can do like this to format

$datetime = DateTime::createFromFormat('d.m.y', '12.07.16');
echo $datetime->format('d.m.Y');