Search code examples
phpdateleap-year

How Display Month which is Start with Current Month Using PHP?


I want to display all month with current year but it must be start with current month like below.

I need like if current month October and year 2019 then option list should be start with 2019-10 then all renaming month with current year like below.

Output

<select>
            <option value="">SELECT MONTH</option>
            <option value="2019-10">October-2019</option>     
            <option value="2019-11">November-2019</option>     
            <option value="2019-12">December-2019</option>     
            <option value="2019-01">January-2019</option>     
            <option value="2019-02">February-2019</option>     
            <option value="2019-03">March-2019</option>     
            <option value="2019-04">April-2019</option>     
            <option value="2019-05">May-2019</option>     
            <option value="2019-06">June-2019</option>     
            <option value="2019-07">July-2019</option>     
            <option value="2019-08">August-2019</option>     
            <option value="2019-09">September-2019</option>     
    </select>

Solution

  • It's Working and give me exact output as i want.

    <select>
        <option value="">SELECT MONTH</option>
        <?php
        for ($i = 0; $i < 12; $i++) 
        {
            $getMonth = strtotime(sprintf('%d months', $i));   
            $monthLabel = date('F', $getMonth)."-".date("Y");
            $monthval = date("Y")."-".date('m', $getMonth); ?>
            <option value="<?php echo $monthval; ?>"><?php echo $monthLabel; ?></option>
        <?php } 
        ?>
    </select>