increment date by one month

前端 未结 18 2273
广开言路
广开言路 2020-11-27 04:14

Let\'s say I have a date in the following format: 2010-12-11 (year-mon-day)

With PHP, I want to increment the date by one month, and I want the year to be automatica

相关标签:
18条回答
  • 2020-11-27 04:25
    function dayOfWeek($date){
        return DateTime::createFromFormat('Y-m-d', $date)->format('N');
    }
    

    Usage examples:

    echo dayOfWeek(2016-12-22);
    // "4"
    echo dayOfWeek(date('Y-m-d'));
    // "4"
    
    0 讨论(0)
  • 2020-11-27 04:28
    $date = strtotime("2017-12-11");
    $newDate = date("Y-m-d", strtotime("+1 month", $date));
    

    If you want to increment by days you can also do it

    $date = strtotime("2017-12-11");
    $newDate = date("Y-m-d", strtotime("+5 day", $date));
    
    0 讨论(0)
  • 2020-11-27 04:29

    put a date in input box then click the button get day from date in jquery

    $(document).ready( function() {
        $("button").click(function(){   
        var day = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
        var a = new Date();
        $(".result").text(day[a.getDay()]);
    
        });  
                 });
    
    0 讨论(0)
  • 2020-11-27 04:31
    strtotime( "+1 month", strtotime( $time ) );
    

    this returns a timestamp that can be used with the date function

    0 讨论(0)
  • 2020-11-27 04:32

    Thanks Jason, your post was very helpful. I reformatted it and added more comments to help me understand it all. In case that helps anyone, I have posted it here:

    function cycle_end_date($cycle_start_date, $months) {
        $cycle_start_date_object = new DateTime($cycle_start_date);
    
        //Find the date interval that we will need to add to the start date
        $date_interval = find_date_interval($months, $cycle_start_date_object);
    
        //Add this date interval to the current date (the DateTime class handles remaining complexity like year-ends)
        $cycle_end_date_object = $cycle_start_date_object->add($date_interval);
    
        //Subtract (sub) 1 day from date
        $cycle_end_date_object->sub(new DateInterval('P1D')); 
    
        //Format final date to Y-m-d
        $cycle_end_date = $cycle_end_date_object->format('Y-m-d'); 
    
        return $cycle_end_date;
    }
    
    //Find the date interval we need to add to start date to get end date
    function find_date_interval($n_months, DateTime $cycle_start_date_object) {
        //Create new datetime object identical to inputted one
        $date_of_last_day_next_month = new DateTime($cycle_start_date_object->format('Y-m-d'));
    
        //And modify it so it is the date of the last day of the next month
        $date_of_last_day_next_month->modify('last day of +'.$n_months.' month');
    
        //If the day of inputted date (e.g. 31) is greater than last day of next month (e.g. 28)
        if($cycle_start_date_object->format('d') > $date_of_last_day_next_month->format('d')) {
            //Return a DateInterval object equal to the number of days difference
            return $cycle_start_date_object->diff($date_of_last_day_next_month);
        //Otherwise the date is easy and we can just add a month to it
        } else {
            //Return a DateInterval object equal to a period (P) of 1 month (M)
            return new DateInterval('P'.$n_months.'M');
        }
    }
    
    $cycle_start_date = '2014-01-31'; // select date in Y-m-d format
    $n_months = 1; // choose how many months you want to move ahead
    $cycle_end_date = cycle_end_date($cycle_start_date, $n_months); // output: 2014-07-02
    
    0 讨论(0)
  • 2020-11-27 04:34
    (date('d') > 28) ? date("mdY", strtotime("last day of next month")) : date("mdY", strtotime("+1 month"));
    

    This will compensate for February and the other 31 day months. You could of course do a lot more checking to to get more exact for 'this day next month' relative date formats (which does not work sadly, see below), and you could just as well use DateTime.

    Both DateInterval('P1M') and strtotime("+1 month") are essentially blindly adding 31 days regardless of the number of days in the following month.

    • 2010-01-31 => March 3rd
    • 2012-01-31 => March 2nd (leap year)
    0 讨论(0)
提交回复
热议问题