PHP: Populating an array with the names of the next 12 months

后端 未结 8 2157
遇见更好的自我
遇见更好的自我 2020-12-03 18:45
for($x=0; $x<12; $x++)
{
    $month = mktime(0, 0, 0, date(\"m\")+$x, date(\"d\"),  date(\"Y\"));
    $key = date(\'m\', $month);
    $monthname = date(\'F\', $mo         


        
8条回答
  •  春和景丽
    2020-12-03 19:42

    Here is a simple script to go forward 12 months from the current date. It includes the year with it.

    # Set Number of Months to Traverse
    $num_months = 12;
    
    # Set Current Month as the 1st
    $current_month = date('Y-m').'-01';
    
    for ($count = 0; $count <= $num_months; $count++) {
        # Fetch Date for each as YYYY-MM-01
        $dates[] = date('Y-m', strtotime($current_month.' + '.$count.' Months')).'-01';
    }
    

    You could turn this into a select list with the current month selected by dropping this in:

    echo '';
    

提交回复
热议问题