PHP的strtotime函数如何一次性同时加天数、分钟数和秒数?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP的strtotime函数如何一次性同时加天数、分钟数和秒数?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP的strtotime函数如何一次性同时加天数、分钟数和秒数?》要点:
本文介绍了PHP的strtotime函数如何一次性同时加天数、分钟数和秒数?,希望对您有用。如果有疑问,可以联系我们。

小编很喜欢PHP的strtotime函数,大凡时间加减的运算,一般都会先想到它。实在妙用无穷。

不过,一般大家都知道使用strtotime做加减时间运算,比如:

echo "今天:".date("Y-m-d")."\n";       

echo "昨天:".date("Y-m-d",strtotime("-1 day")). "\n";       

echo "明天:".date("Y-m-d",strtotime("+1 day")). "\n";    

echo "一周后:".date("Y-m-d",strtotime("+1 week")). "\n";       

echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "\n";       

echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "\n";       

echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."\n";       

echo "一个月前:".date("Y-m-d",strtotime("last month"))."\n";       

echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."\n";       

echo "十年后:".date("Y-m-d",strtotime("+10 year"))."\n";   

但是,假如你同时要加1天,23分钟,59秒,那是不是要分三步?

不要,只要一步即可,方法就是在strtotime的第一个参数用逗号相隔即可,加减不分先后。

比如,下面是对一个指定的$day加1天59分59秒。写法就是用逗号连接“+59 minute,+1 hour,+59 second”,其中,故意把+1 hour加到中间都不影响

$day = '2018-04-21 18:00:00';
$next = date ('Y-m-d H:i:s', strtotime ('+59 ;minute, +1 hour,+59 second', strtotime ($day)));

输出

2018-04-21 19:59:59

好用吧?

脚本宝典总结

以上是脚本宝典为你收集整理的PHP的strtotime函数如何一次性同时加天数、分钟数和秒数?全部内容,希望文章能够帮你解决PHP的strtotime函数如何一次性同时加天数、分钟数和秒数?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:phpstrtotime