PHP date; How to find the next year?

前端 未结 8 1682
生来不讨喜
生来不讨喜 2021-01-07 18:27

I want to get today\'s date + one year. How do I achieve this with PHP\'s date functions?

相关标签:
8条回答
  • 2021-01-07 18:48

    You could use the new Datetime and Datetime_Intervall-classes introduced in the later PHP 5-versions.

    I once posted an answer in this question. Maybe it helps you :)

    The advantage is, that this classes also checks for leap-seconds and leap-years, timezones, etc.

    0 讨论(0)
  • 2021-01-07 18:49

    From PHP's documentation:

    <?php
        $date = new DateTime($your_supposed_date);
        $date->add(new DateInterval('P1Y'));
        echo $date->format('Y-m-d') . "\n";
    ?>
    

    Gordon's much cleaner version (Thank you!):

    <?php
        $date = new DateTime("+12 months $theDate");
        echo $date->format('Y-m-d') . "\n";
    ?>
    
    0 讨论(0)
提交回复
热议问题