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


    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>';

    执行效果:

  • 相关阅读:
    小艾电台-小众音乐科普讲座
    永乐计分器
    顺金斗花牌-比大小
    Bigger_0305
    iTunes Connect后台无法创建App的解决方案
    iOS navigationBar导航栏底部与self.view的分界线的隐藏
    iOS 十六进制的相加取反
    UITabBar-UITabBarItem图片的背景颜色属性和文字的颜色大小设置
    iOS GCD多线程介绍
    [POJ3461] Oulipo
  • 原文地址:https://www.cnblogs.com/-mrl/p/11777671.html
Copyright © 2020-2023  润新知