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

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

    You can use PHP's strtotime() function:

    // One month from today
    $date = date('Y-m-d', strtotime('+1 month'));
    
    // One month from a specific date
    $date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
    

    Just note that +1 month is not always calculated intuitively. It appears to always add the number of days that exist in the current month.

    Current Date  | +1 month
    -----------------------------------------------------
    2015-01-01    | 2015-02-01   (+31 days)
    2015-01-15    | 2015-02-15   (+31 days)
    2015-01-30    | 2015-03-02   (+31 days, skips Feb)
    2015-01-31    | 2015-03-03   (+31 days, skips Feb)
    2015-02-15    | 2015-03-15   (+28 days)
    2015-03-31    | 2015-05-01   (+31 days, skips April)
    2015-12-31    | 2016-01-31   (+31 days)
    

    Some other date/time intervals that you can use:

    $date = date('Y-m-d'); // Initial date string to use in calculation
    
    $date = date('Y-m-d', strtotime('+1 day', strtotime($date)));
    $date = date('Y-m-d', strtotime('+1 week', strtotime($date)));
    $date = date('Y-m-d', strtotime('+2 week', strtotime($date)));
    $date = date('Y-m-d', strtotime('+1 month', strtotime($date)));
    $date = date('Y-m-d', strtotime('+30 days', strtotime($date)));
    

提交回复
热议问题