>  기사  >  백엔드 개발  >  php strtotime 求解释

php strtotime 求解释

WBOY
WBOY원래의
2016-06-23 13:39:17936검색

$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 日print_r(array(            date('Y年m月',strtotime($t)),            date('Y年m月',strtotime('+ 1 month',strtotime($t))),            date('Y年m月',strtotime('+ 2 month',strtotime($t))),));
Array(    [0] => 2011年01月    [1] => 2011年02月    [2] => 2011年03月)


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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.