只有年份的 strtotime() 返回错误数据 [英] strtotime() with only year return wrong data
本文介绍了只有年份的 strtotime() 返回错误数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下返回正确日期
echo date('Y-m',strtotime('2009-3'));
但是这只是一年时返回错误的数据
But this returning wrong data when just year
echo date('Y',strtotime('2009'));
显示当前年份.那里有什么问题
This showing current year. Whats wrong there
推荐答案
因为只出了2009" strtotime()
无法创建有效的 Unix 时间戳.所以你需要像这样传递一个完整的日期:
Because only out of "2009" strtotime()
can't create a valid Unix timestamp. So you need to pass a full date like this:
echo date('Y',strtotime('2009-04-07'));
//^^^^^^^^^^
手册中的引用也显示了这一点:
Also a quote from the manual shows this too:
函数期望得到一个包含英文日期格式的字符串,并将尝试将该格式解析为Unix时间戳
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp
如果您只有年份,您可以使用 DateTime::createFromFormat,像这样:
If you only have the year you can use DateTime::createFromFormat, like this:
$date = DateTime::createFromFormat("Y", "2009");
echo $date->format("Y");
输出:
2009
这篇关于只有年份的 strtotime() 返回错误数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文