Home  >  Article  >  Backend Development  >  php strtotime 求解释,该如何解决

php strtotime 求解释,该如何解决

WBOY
WBOYOriginal
2016-06-13 12:15:37831browse

php strtotime 求解释
$t = '2011-01-31 17:21:22';
print_r(array(
            date('Y年m月',strtotime($t)),
            date('Y年m月',strtotime('+ 1 month',strtotime($t))),
            date('Y年m月',strtotime('+ 2 month',strtotime($t))),
));
exit;
为什么都是3月。。
------解决思路----------------------
$t 时间是1月31号  2月没有  所以数组第二个元素也是三月了。
------解决思路----------------------
二月没有 31 日 
所以 一月31日加一个月就到三月了

$t = '2011-01-28 17:21:22'; //改成 28 日<br />print_r(array(<br />            date('Y年m月',strtotime($t)),<br />            date('Y年m月',strtotime('+ 1 month',strtotime($t))),<br />            date('Y年m月',strtotime('+ 2 month',strtotime($t))),<br />));
Array<br />(<br />    [0] => 2011年01月<br />    [1] => 2011年02月<br />    [2] => 2011年03月<br />)<br /><br />


strtotime 是有自动校正日期的功能的
print_r(array(<br />            date('Y年m月d日',strtotime('2011-02-31')),<br />            date('Y年m月d日',strtotime('2011-04-31')),<br />            date('Y年m月d日',strtotime('2011-06-31')),<br />));
Array<br />(<br />    [0] => 2011年03月03日<br />    [1] => 2011年05月01日<br />    [2] => 2011年07月01日<br />)<br /><br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn