• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP 指定时间/时间戳+某天/某月/某年

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

PHP指定时间戳加上1天,1周,1月,一年其实是不需要用上什么函数的!指定时间戳本身就是数字整型,我们只需要再计算1天,1周它的秒数相加即可!

博主搜索php指定时间戳加一天一年,结果许多的文章给出来的结果是用的函数:strtotime();这个函数的确是可以很好的帮助到你,用这个函数有两种方法可以实现:

第一:是你需要先把指定时间戳格式化再用这个函数才能加一天,一年.....;而且网上各大平台都没有讲到这个知识点!

第二:直接用这个函数的第2个参数,很多人用这个函数基本上是不用第2个参数的,默认的第2个参数是得到当前时间戳,同时我们也可以自定义时间戳的;这个知识点互联网上更加的没有讲解到,连PHP手册也没有讲解到吧!

<?php
//http://127.0.0.1/date.php
echo '当前时间戳例子:';
echo '<br>';
echo '当前时间戳:' . date('Y-m-d H:i:s', strtotime('now'));//当前时间戳 2017-01-09 21:04:11
echo '<br>';
echo '当前时间戳+1秒:' . date('Y-m-d H:i:s', strtotime('+1second'));//当前时间戳+1秒 2017-01-09 21:04:12
echo '<br>';
echo '当前时间戳+1分:' . date('Y-m-d H:i:s', strtotime('+1minute'));//当前时间戳+1分 2017-01-09 21:05:11
echo '<br>';
echo '当前时间戳+1小时:' . date('Y-m-d H:i:s', strtotime('+1hour'));//当前时间戳+1小时 2017-01-09 22:04:11
echo '<br>';
echo '当前时间戳+1天:' . date('Y-m-d H:i:s', strtotime('+1day'));//当前时间戳+1天 2017-01-10 21:04:11
echo '<br>';
echo '当前时间戳+1周:' . date('Y-m-d H:i:s', strtotime('+1week'));//当前时间戳+1周 2017-01-16 21:04:11
echo '<br>';
echo '当前时间戳+1月:' . date('Y-m-d H:i:s', strtotime('+1month'));//当前时间戳+1月 2017-02-09 21:04:11
echo '<br>';
echo '当前时间戳+1年:' . date('Y-m-d H:i:s', strtotime('+1year'));//当前时间戳+1年 2018-01-09 21:04:11
echo '<br>';
echo '当前时间戳+12年,12月,12天,12小时,12分,12秒:' . date('Y-m-d H:i:s', strtotime('+12year 12month 12day 12hour 12minute 12second'));//当前时间戳+12年,12月,12天,12小时,12分,12秒 2030-01-22 09:16:23
echo '<br>';
echo '<br>';
echo '指定时间戳例子:';
echo '<br>';
$t = 1575302400;//指定时间戳
echo '指定时间:' . $dt = date('Y-m-d H:i:s', $t);//2019-12-02 16:00:00
echo '<br>';
echo '<br>';
/*方法一*/
echo '指定时间+1天:' . date('Y-m-d H:i:s', $t + 1 * 24 * 60 * 60);//指定时间+1天:2019-12-03 16:00:00
echo '<br>';
echo '指定时间+1月:' . date('Y-m-d H:i:s', $t + 31 * 24 * 60 * 60);//指定时间+1月:2020-01-02 16:00:00
echo '<br>';
echo '指定时间+1年:' . date('Y-m-d H:i:s', $t + 365 * 24 * 60 * 60);//指定时间+1年:2020-12-01 16:00:00
echo '<br>';
echo '<br>';
/*方法二*/
//$dt是指定时间戳格式化后的日期
echo '指定时间+1天:' . date('Y-m-d H:i:s', strtotime("$dt+1day"));//指定时间+1天:2019-12-03 16:00:00
echo '<br>';
echo '指定时间+1月:' . date('Y-m-d H:i:s', strtotime("$dt+1month"));//指定时间+1月:2020-01-02 16:00:00
echo '<br>';
echo '指定时间+1年:' . date('Y-m-d H:i:s', strtotime("$dt+1year"));//指定时间+1年:2020-12-02 16:00:00
echo '<br>';
echo '<br>';
/*方法三*/
//$t是指定时间戳
echo '指定时间+1天:' . date('Y-m-d H:i:s', strtotime("+1day", $t));//指定时间+1天:2019-12-03 16:00:00
echo '<br>';
echo '指定时间+1月:' . date('Y-m-d H:i:s', strtotime("+1month", $t));//指定时间+1月:2020-01-02 16:00:00
echo '<br>';
echo '指定时间+1年:' . date('Y-m-d H:i:s', strtotime("+1year", $t));//指定时间+1年:2020-12-02 16:00:00
echo '<br>';
echo '<br>';

 

执行效果:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
centos7基于Apache+php+mysql 搭建简单网站发布时间:2022-07-10
下一篇:
PHP获取搜索引擎关键词发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap