How do I get next month date from today's date and insert it in my database?

后端 未结 13 566
我寻月下人不归
我寻月下人不归 2021-02-01 15:07

I have two columns in my db: start_date and end_date, which are both DATE types. My code is updating the dates as follows:



        
相关标签:
13条回答
  • 2021-02-01 15:23

    The accepted answer works only if you want exactly 31 days later. That means if you are using the date "2013-05-31" that you expect to not be in June which is not what I wanted.

    If you want to have the next month, I suggest you to use the current year and month but keep using the 1st.

    $date = date("Y-m-01");
    $newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;
    

    This way, you will be able to get the month and year of the next month without having a month skipped.

    0 讨论(0)
  • 2021-02-01 15:23

    You do not actually need PHP functions to achieve this. You can already do simple date manipulations directly in SQL, for example:

    $sql1 = "
        UPDATE `users` SET 
        `start_date` = CURDATE(), 
        `end_date` = DATE_ADD(CURDATE(), INTERVAL 1 MONTH)  
        WHERE `users`.`id` = '" . $id . "';
    ";
    

    Refer to http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime

    0 讨论(0)
  • 2021-02-01 15:23

    Be careful, because sometimes strtotime("+1 months") can skip month numbers.

    Example:

    $today = date("Y-m-d"); // 2012-01-30
    $next_month = date("Y-m-d", strtotime("$today +1 month"));
    

    If today is January, next month should be February which has 28 or 29 days, but PHP will return March as next month, not February.

    0 讨论(0)
  • 2021-02-01 15:23

    This function returns any correct number of months positively or negatively. Found in the comment section here:

    function addMonthsToTime($numMonths = 1, $timeStamp = null){
        $timeStamp === null and $timeStamp = time();//Default to the present
        $newMonthNumDays =  date('d',strtotime('last day of '.$numMonths.' months', $timeStamp));//Number of days in the new month
        $currentDayOfMonth = date('d',$timeStamp);
    
        if($currentDayOfMonth > $newMonthNumDays){
          $newTimeStamp = strtotime('-'.($currentDayOfMonth - $newMonthNumDays).' days '.$numMonths.' months', $timeStamp);
        } else {
        $newTimeStamp = strtotime($numMonths.' months', $timeStamp);
        }
    
        return $newTimeStamp;
    }
    
    0 讨论(0)
  • 2021-02-01 15:30

    If you want a specific date in next month, you can do this:

    // Calculate the timestamp
    $expire = strtotime('first day of +1 month');
    
    // Format the timestamp as a string
    echo date('m/d/Y', $expire);
    

    Note that this actually works more reliably where +1 month can be confusing. For example...

    Current Day   | first day of +1 month     | +1 month
    ---------------------------------------------------------------------------
    2015-01-01    | 2015-02-01                | 2015-02-01
    2015-01-30    | 2015-02-01                | 2015-03-02  (skips Feb)
    2015-01-31    | 2015-02-01                | 2015-03-03  (skips Feb)
    2015-03-31    | 2015-04-01                | 2015-05-01  (skips April)
    2015-12-31    | 2016-01-01                | 2016-01-31
    
    0 讨论(0)
  • 2021-02-01 15:32
    date("Y-m-d",strtotime("last day of +1 month",strtotime($anydate)))
    
    0 讨论(0)
提交回复
热议问题