首页 > php教程 > php手册 > 正文

strtotime() PHP中的其他用途 上月下月时间不准确

WBOY
发布: 2016-06-06 20:11:21
原创
1749 人浏览过

当我们使用PHP获得上个月的时候,一般会使用strtotime('-1 MONTH'),但是这样有时候会不准确,比如 今天是2013-03-31,strtotime('-1 MONTH')的结果是2013-03-03,而不是预期的2013年2月的日期,初步设想是因为这个方法是先查找上个月的天数,然后在使用今天

当我们使用PHP获得上个月的时候,一般会使用strtotime('-1 MONTH'),但是这样有时候会不准确,比如 今天是2013-03-31,strtotime('-1 MONTH')的结果是2013-03-03,而不是预期的2013年2月的日期,初步设想是因为这个方法是先查找上个月的天数,然后在使用今天的时间减去上个月天数。。。

以下有几种方法,可以帮助我们达到预期效果,比如我要返回上个月的月份:
echo date('M Y', strtotime('midnight first day of -1 month'));
或者:
echo date('M Y', strtotime(date('Y-m-01')) - 86400);

下方是其他的用途:

strtotime('first day of last month');
strtotime('last day of last month');
strtotime('first of last week');
strtotime('first of this week');
strtotime('this week midnight'); // returns Monday midnight of this week
strtotime('last week midnight'); // returns Monday midnight of last week
strtotime('last week Sunday midnight'); // returns Sunday midnight of this week
strtotime('-2 weeks Sunday midnight'); // returns Sunday midnight of last week

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)),
));
?>

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!