我是靠谱客的博主 含糊飞机,这篇文章主要介绍php strtotime month,PHP 关于strtotime(" x month") bug的解决,现在分享给大家,希望可以做个参考。

PHP 关于strtotime("- x month") bug的解决

strtotime('-x month'); 在涉及到月份修改的时候,可能不会得到预料的结果。

此为php的一个bug:

https://bugs.php.net/bug.php?id=27793

如:当前时间为: 2011-08-31 17:21:22

date_default_timezone_set('Asia/Shanghai');

$t = time();

print_r(array(

date('Y年m月',$t),

date('Y年m月',strtotime('- 1 month',$t)),

date('Y年m月',strtotime('- 2 month',$t)),

));

?>

上面代码输出:

Array

(

[0] => 2011年08月

[1] => 2011年07月

[2] => 2011年07月

)

而预期的结果是:

Array

(

[0] => 2011年08月

[1] => 2011年07月

[2] => 2011年06月

)

============================================

可以用如下方法解决:

date_default_timezone_set('Asia/Shanghai');

$first_day_of_month = date('Y-m',time()) . '-01 00:00:01';

$t = strtotime($first_day_of_month);

print_r(array(

date('Y年m月',$t),

date('Y年m月',strtotime('- 1 month',$t)),

date('Y年m月',strtotime('- 2 month',$t)),

));

?>

输出:

Array

(

[0] => 2011年08月

[1] => 2011年07月

[2] => 2011年06月

)

最后

以上就是含糊飞机最近收集整理的关于php strtotime month,PHP 关于strtotime(" x month") bug的解决的全部内容,更多相关php内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部