问题:
一:获取下个月第一天日期?
二:获取本月最后一天最后一秒的时间?
三:获取10年前本月的开始时间?
四:获取下月第一个星期天?
五:获取本月第二个星期五
六:获取两周前的今天的日期
七:获取5天前的日期,排除非工作日(排除周末)
八:获取下周星期六的日期?
解答:
一:如何获取下个月第一天日期?
date('Y-m-01',strtotime('+1 month')); //错误,strtotime('+1 month')如果当前日期的一个月不存在,如2017-08-31日,则strtotime('+1 month')取的是2017-10-01;当不存在时会取下一个月的第一天。
strtotime('fist day of +1 month'); //正确
二:如何获取本月最后一天最后一秒的时间?
strtotime(date('Y-m-01 00:00:00', strtotime('+1 month'))) - 1); //错误,同问题一
date('Y-m-d 23:59:59', strtotime('last day of this month')); //正确,如果是下个月 date('Y-m-d 23:59:59', strtotime('last day of -1 month'));
三:如何获取10年前本月的开始时间?
date('Y-m-d 00:00:00', strtotime('-10 year')); //错误:strtotime('-10 year') 如果当前是2016-02-29,则strtotime('-10 year')实际取到的是2006-03-01,因为2006年没有2月29号
strtotime('fist day of -120 month'); //正确
四:如何获取下月第一个星期天?
strtotime('first sunday of +1 month'); // 同理获取下月最后一个星期天 strtotime('last sunday of +1 month');
五:如何获取本月第二个星期五
strtotime('second friday of this month'); // 同理获取第三个星期几 "third ... of ..."
六:如何获取两周前的今天的日期
strtotime('-2 weeks'); // 同理获取第三个星期几 "third ... of ..."
七:如何获取5天前的日期,排除非工作日(排除周末)
strtotime('-5 weekdays');
八:如何获取下周星期六的日期?
strtotime('saturday +1 week');
总结:
PHP神函数之一 strtotime
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。