为什么 strtotime('2016-01-01') 结果为 1.1.2015?
Why does strtotime('2016-01-01') result to 1.1.2015?
我的应用程序中报告了一个错误,我在 mysql 日期 return 从数据库中使用了 strtotime 函数。一切正常,直到今天出现错误。
当我运行:
date('j.n.o', strtotime('2016-01-01'))
出于某种原因我得到结果“1.1.2015”。
但是如果我 运行
date('j.n.o', strtotime('2016-02-01'))
我得到“1.2.2016”,这是正确的,但我不知道第一个示例失败的原因。
我可以很容易地做一个解决方法来使它正常工作,但我特别想知道为什么这个函数 return 这个输入会产生如此奇怪的结果?我检查了 php 5.3 和 7.0,得到了相同的结果
尝试使用 Y
而不是 o
作为年份
echo date('j.n.Y', strtotime('2016-01-01'));
http://php.net/manual/en/function.date.php
o --- ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
对比
Y --- A full numeric representation of a year, 4 digits
试试这个:
PHP 包含用于格式化给定日期或时间的内置日期函数。这些是,
日期()
date_format()
此函数接受两个可选参数。即格式和时间戳。该代码显示了 PHP date() 函数的示例。
/ returns 时间戳日期,格式为 dd/mm/YYYY
回声日期("d-m-Y",时间());
我的应用程序中报告了一个错误,我在 mysql 日期 return 从数据库中使用了 strtotime 函数。一切正常,直到今天出现错误。
当我运行:
date('j.n.o', strtotime('2016-01-01'))
出于某种原因我得到结果“1.1.2015”。
但是如果我 运行
date('j.n.o', strtotime('2016-02-01'))
我得到“1.2.2016”,这是正确的,但我不知道第一个示例失败的原因。
我可以很容易地做一个解决方法来使它正常工作,但我特别想知道为什么这个函数 return 这个输入会产生如此奇怪的结果?我检查了 php 5.3 和 7.0,得到了相同的结果
尝试使用 Y
而不是 o
作为年份
echo date('j.n.Y', strtotime('2016-01-01'));
http://php.net/manual/en/function.date.php
o --- ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
对比
Y --- A full numeric representation of a year, 4 digits
试试这个:
PHP 包含用于格式化给定日期或时间的内置日期函数。这些是,
日期() date_format()
此函数接受两个可选参数。即格式和时间戳。该代码显示了 PHP date() 函数的示例。
/ returns 时间戳日期,格式为 dd/mm/YYYY 回声日期("d-m-Y",时间());