#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,672
    Rep Power
    171

    Why does this dropdown show 2 March and no February?


    Why does this drop down show March twice and February not at all? Thanks
    PHP Code:
    public function index($name=NULL
                {
                    
    $CI =& get_instance();
                    
    $CI->posted_month $CI->input->post($name) ? $CI->input->post($name) : "";
                    
    $m 0;
                    
    $CI->dropdown="<select name=\"".$name."\">";
                    while(
    $m<12)
                        {
                            
    $m++;
                            
    $CI->dropdown.="<option value=\"".$m."\"";
                            if(
    $CI->posted_month==$m)
                                {
                                    
    $CI->dropdown.="selected=\"selected\"";
                                }
                            
    $CI->dropdown.= ">\n".date("F"mktime(000$m))."</option>";
                        }
                    
    $CI->dropdown.="</select>";    
                    return 
    $CI->dropdown;
                } 
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Because you told mktime() to create a date for the 30th of February.

    Pass in 1 for the day too.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I'd consider switching to the DateTime class, which is less low-level and easier to use:
    PHP Code:
    $aux_date = new DateTime('2013-1-1');
    for (
    $i 1$i <= 12$i++) {
        echo 
    $aux_date->format('F');
        
    $aux_date->modify('+1 month');

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Here's one I use which submit onChange, holds its value, and starts with current month selected.

    PHP Code:
          <select name="mo" onChange="this.form.submit()">
    <?php
              $current_date 
    getdate();
              for(
    $i 1$i <= 12$i++) {
                
    $selected = (isset($_POST['mo']) && $_POST['mo'] == $i) || (!isset($_POST['mo']) && $i == $current_date['mon']);
                
    $year = ($i <= $current_date['mon'] ? date("Y") : date("Y") - 1);
                echo 
    '        <option value="' $i  '"' . ($selected ' selected="selected"' '') . '>' date("F"mktime(000$i10)) . ' - ' $year '</option>\n';
              }
            
    ?>
          </select>

IMN logo majestic logo threadwatch logo seochat tools logo