get current date and date after two months in php

后端 未结 3 1281
悲&欢浪女
悲&欢浪女 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:30

    There are a couple of ways you could go about it - the first one would be to do something like this:

    echo $currentdate = date("Y-m-d H:i:s",time());    
    echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);
    

    Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months.

    Another way to do it, which is my preferred way and how I would do it, is this:

    $after60days = strtotime("+60 days");
    

    The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function.

    Of course, if you need to output a date in a format that easy to read for humans, you can do something like this:

    echo date('Y-m-d H:i:s',$after60days);
    
    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2020-12-19 12:37

    Today:

    date('m-d-Y', time());

    Two months from now:

    date('m-d-Y', time() + (86400 * 60));

    0 讨论(0)
提交回复
热议问题