get current date and date after two months in php

后端 未结 3 1291
悲&欢浪女
悲&欢浪女 2020-12-19 12:10

this is a very lame question but i m not able to find this one. How to get today\'s date and date after two months..

format is month-date-year (numerical.)

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 12:33

    You can use the strtotime() function :

    $today = time();
    $twoMonthsLater = strtotime("+2 months", $today);
    
    // If what you really want is exactly 60 days later, then
    $sixtyDaysLater = strtotime("+60 days", $today);
    // ...or 8 weeks later :
    $eightWeeksLater = strtotime("+8 weeks", $today);
    

    In any case, the resulting new timestamps can then be converted to month-date-year :

    echo 'Today is : ' . date('m-d-Y', $today);
    echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);
    

    ** UPDATE **

    From the PHP manual

    Note: Please keep in mind that these functions are dependent on the locale settings of your server. Make sure to take daylight saving time (use e.g. $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions.

    Just thought I should mention it...

提交回复
热议问题