Thursday, April 28, 2011

get current date and date after two months in php.

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.)

From stackoverflow
  • 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...

  • Today:

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

    Two months from now:

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

    Yanick Rochon : this is not true. '2010-02-01' (Feb. 1, 2010) + 60 days is not exactly two months later. And this does not take into account leap years, etc.

0 comments:

Post a Comment