给你5分钟做完这8道日期题

问题:

一:获取下个月第一天日期?

二:获取本月最后一天最后一秒的时间?

三:获取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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值