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

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

    I know - sort of late. But I was working at the same problem. If a client buys a month of service, he/she expects to end it a month later. Here's how I solved it:

        $now = time(); 
        $day = date('j',$now);
        $year = date('o',$now);
        $month = date('n',$now);
        $hour = date('G');
        $minute = date('i');
        $month += $count;
        if ($month > 12)        {
                $month -= 12;
                $year++; 
                }
        $work = strtotime($year . "-" . $month . "-01");
        $avail = date('t',$work);
        if ($day > $avail)
                $day = $avail;
        $stamp = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);
    

    This will calculate the exact day n*count months from now (where count <= 12). If the service started March 31, 2019 and runs for 11 months, it will end on Feb 29, 2020. If it runs for just one month, the end date is Apr 30, 2019.

提交回复
热议问题