PHP date add 5 year to current date

后端 未结 10 2083
野的像风
野的像风 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: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

        
    

提交回复
热议问题