PHP date add 5 year to current date

后端 未结 10 2039
野的像风
野的像风 2020-12-01 03:47

I have this PHP code:

$end=date(\'Y-m-d\');

I use it to get the current date, and I need the date 5 years in the future, something like:

相关标签:
10条回答
  • 2020-12-01 04:09

    To add one year to todays date use the following:

    $oneYearOn = date('Y-m-d',strtotime(date("Y-m-d", mktime()) . " + 365 day"));
    
    0 讨论(0)
  • 2020-12-01 04:10
           $date = strtotime($row['timestamp']);
           $newdate = date('d-m-Y',strtotime("+1 year",$date));
    
    0 讨论(0)
  • 2020-12-01 04:12

    Modifying dates based on this post
    strtotime() is really powerful and allows you to modify/transform dates easily with it’s relative expressions too:

    Procedural

        $dateString = '2011-05-01 09:22:34';
        $t = strtotime($dateString);
        $t2 = strtotime('-3 days', $t);
        echo date('r', $t2) . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
    

    DateTime

        $dateString = '2011-05-01 09:22:34';
        $dt = new DateTime($dateString);
        $dt->modify('-3 days');
        echo $dt->format('r') . PHP_EOL; // returns: Thu, 28 Apr 2011 09:22:34 +0100
    

    The stuff you can throw at strtotime() is quite surprising and very human readable. Have a look at this example looking for Tuesday next week.

    Procedural

        $t = strtotime("Tuesday next week");
        echo date('r', $t) . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
    

    DateTime

        $dt = new DateTime("Tuesday next week");
        echo $dt->format('r') . PHP_EOL; // returns: Tue, 10 May 2011 00:00:00 +0100
    

    Note that these examples above are being returned relative to the time now. The full list of time formats that strtotime() and the DateTime constructor takes are listed on the PHP Supported Date and Time Formats page.

    Another example, suitable for your case could be: based on this post

        <?php
        //How to get the day 3 days from now:
        $today = date("j");
        $thisMonth = date("n");
        $thisYear = date("Y");
        echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear)); 
    
        //1 week from now:
        list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
        echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));
    
        //4 months from now:
        list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
        echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear)); 
    
        //3 years, 2 months and 35 days from now:
        list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
        echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
        ?>
    
    0 讨论(0)
  • 2020-12-01 04:13

    Try this code and add next Days, Months and Years

    // current month: Aug 2018
    $n = 2;
    for ($i = 0; $i <= $n; $i++){
       $d = strtotime("$i days");
       $x = strtotime("$i month");
       $y = strtotime("$i year");
       echo "Dates : ".$dates = date('d M Y', "+$d days");
       echo "<br>";
       echo "Months : ".$months = date('M Y', "+$x months");
       echo '<br>';
       echo "Years : ".$years = date('Y', "+$y years");
       echo '<br>';
    }
    
    0 讨论(0)
提交回复
热议问题