Friday, 3 June 2016

Year dropdown from current year to 2013 and Month dropdown with leading zero

Year Dropdown from current year to 2013
<?php
$earliest_year = 2013;
$latest_year = date('Y');
?>
<select name="filterYear" id="filterYear"  style="width:130px">            
         <?php
             foreach ( range( $latest_year, $earliest_year ) as $i ) {
         ?>
         <option value="<?php echo $i; ?>" <?php echo ((int)$i === (int)$filterYear ? ' selected="selected"' : ''); ?>><?php echo $i; ?></option>
         <?php
         }
         ?>
         </select>
Month Dropdown with leading zero upto 10th month
<?php
$numZeroesToAppend =2;
?>
<select name="filterMonth" id="filterMonth"  style="width:130px">
               <?php
                   for($m=1;$m<=12;$m++){       

                    $mUpdated = str_pad($m, $numZeroesToAppend, '0', STR_PAD_LEFT);
              ?>
                <option <?php echo ((int)$filterMonth == (int)$mUpdated)?"selected":""; ?> value="<?php echo $mUpdated; ?>"><?php echo $mUpdated; ?></option>
              <?php
                }
                ?>
               </select>

0 comments:

Post a Comment