increment date by one month

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

    //ECHO MONTHS BETWEEN TWO TIMESTAMPS
    $my_earliest_timestamp = 1532095200;
    $my_latest_timestamp = 1554991200;
    
    echo '
    ';
    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;
    }
    

提交回复
热议问题