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

后端 未结 13 592
我寻月下人不归
我寻月下人不归 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

    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;
    }
    

提交回复
热议问题