increment date by one month

前端 未结 18 2276
广开言路
广开言路 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:43

    Use DateTime::add.

    $start = new DateTime("2010-12-11", new DateTimeZone("UTC"));
    $month_later = clone $start;
    $month_later->add(new DateInterval("P1M"));
    

    I used clone because add modifies the original object, which might not be desired.

    0 讨论(0)
  • 2020-11-27 04:44
    //ECHO MONTHS BETWEEN TWO TIMESTAMPS
    $my_earliest_timestamp = 1532095200;
    $my_latest_timestamp = 1554991200;
    
    echo '<pre>';
    echo "Earliest timestamp: ". date('c',$my_earliest_timestamp) ."\r\n";
    echo "Latest timestamp: " .date('c',$my_latest_timestamp) ."\r\n\r\n";
    
    echo "Month start of earliest timestamp: ". date('c',strtotime('first day of '. date('F Y',$my_earliest_timestamp))) ."\r\n";
    echo "Month start of latest timestamp: " .date('c',strtotime('first day of '. date('F Y',$my_latest_timestamp))) ."\r\n\r\n";
    
    echo "Month end of earliest timestamp: ". date('c',strtotime('last day of '. date('F Y',$my_earliest_timestamp)) + 86399) ."\r\n";
    echo "Month end of latest timestamp: " .date('c',strtotime('last day of '. date('F Y',$my_latest_timestamp)) + 86399) ."\r\n\r\n";
    
    $sMonth = strtotime('first day of '. date('F Y',$my_earliest_timestamp));
    $eMonth = strtotime('last day of '. date('F Y',$my_earliest_timestamp)) + 86399;
    $xMonth = strtotime('+1 month', strtotime('first day of '. date('F Y',$my_latest_timestamp)));
    
    while ($eMonth < $xMonth) {
        echo "Things from ". date('Y-m-d',$sMonth) ." to ". date('Y-m-d',$eMonth) ."\r\n\r\n";
        $sMonth = $eMonth + 1; //add 1 second to bring forward last date into first second of next month.
        $eMonth = strtotime('last day of '. date('F Y',$sMonth)) + 86399;
    }
    
    0 讨论(0)
  • 2020-11-27 04:45
     <?php
                  $selectdata ="select fromd,tod  from register where username='$username'";
                $q=mysqli_query($conm,$selectdata);
                $row=mysqli_fetch_array($q);
    
                $startdate=$row['fromd']; 
                $stdate=date('Y', strtotime($startdate));  
    
                $endate=$row['tod']; 
                $enddate=date('Y', strtotime($endate));  
    
                $years = range ($stdate,$enddate);
                echo '<select name="years" class="form-control">';
                echo '<option>SELECT</option>';
                foreach($years as $year)
                  {   echo '<option value="'.$year.'"> '.$year.' </option>';  }
                    echo '</select>'; ?>
    
    0 讨论(0)
  • 2020-11-27 04:47

    For anyone looking for an answer to any date format.

    echo date_create_from_format('d/m/Y', '15/04/2017')->add(new DateInterval('P1M'))->format('d/m/Y');
    

    Just change the date format.

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

    You can use DateTime::modify like this :

    $date = new DateTime('2010-12-11');
    $date->modify('+1 month');
    

    See documentations :

    http://php.net/manual/fr/datetime.modify.php

    http://php.net/manual/fr/class.datetime.php

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

    All presented solutions are not working properly.
    strtotime() and DateTime::add or DateTime::modify give sometime invalid results.
    Examples:
    - 31.08.2019 + 1 month gives 01.10.2019 instead 30.09.2019
    - 29.02.2020 + 1 year gives 01.03.2021 instead 28.02.2021
    (tested on PHP 5.5, PHP 7.3)

    Below is my function based on idea posted by Angelo that solves the problem:

    // $time - unix time or date in any format accepted by strtotime() e.g. 2020-02-29  
    // $days, $months, $years - values to add
    // returns new date in format 2021-02-28
    function addTime($time, $days, $months, $years)
    {
        // Convert unix time to date format
        if (is_numeric($time))
        $time = date('Y-m-d', $time);
    
        try
        {
            $date_time = new DateTime($time);
        }
        catch (Exception $e)
        {
            echo $e->getMessage();
            exit;
        }
    
        if ($days)
        $date_time->add(new DateInterval('P'.$days.'D'));
    
        // Preserve day number
        if ($months or $years)
        $old_day = $date_time->format('d');
    
        if ($months)
        $date_time->add(new DateInterval('P'.$months.'M'));
    
        if ($years)
        $date_time->add(new DateInterval('P'.$years.'Y'));
    
        // Patch for adding months or years    
        if ($months or $years)
        {
            $new_day = $date_time->format("d");
    
            // The day is changed - set the last day of the previous month
            if ($old_day != $new_day)
            $date_time->sub(new DateInterval('P'.$new_day.'D'));
        }
        // You can chage returned format here
        return $date_time->format('Y-m-d');
    }
    

    Usage examples:

    echo addTime('2020-02-29', 0, 0, 1); // add 1 year (result: 2021-02-28)
    echo addTime('2019-08-31', 0, 1, 0); // add 1 month (result: 2019-09-30)
    echo addTime('2019-03-15', 12, 2, 1); // add 12 days, 2 months, 1 year (result: 2019-09-30)
    
    0 讨论(0)
提交回复
热议问题