Pergunta

To list months in a dropdown is used a php function

<select name="month" class="span3">
        <?php
            for($m = 1;$m <= 12; $m++){ 
                $month =  date("F", mktime(0, 0, 0, $m));?>
                <option  value="<?php echo $m; ?>"><?php echo $month; ?></option> 
        <?php } ?>
    </select>

It was working fine when i check today (i.e 31-07-2013) its showing month as

    <select class="span3" name="month">
       <option value="1">January</option>
       <option value="2">March</option>
       <option value="3">March</option>
       <option value="4">May</option>
       <option value="5">May</option>
       <option value="6">July</option>
       <option value="7">July</option>
       <option value="8">August</option>
       <option value="9">October</option>
       <option value="10">October</option>
       <option value="11">December</option>
       <option value="12">December</option>
    </select>

when i check date format as ('Y-m-d') its showing first and last date of those month and doesn't show February for feb it showing 2013-03-03. When i modify the mktime function as mktime(0, 0, 0, $m,1,2013) the results coming as properly as expected. Is there anything i need to look for when i use mktime() or date() function in php.

Foi útil?

Solução

Add the 5th parameter to fix this.

$month =  date("F", mktime(0, 0, 0, $m, 1));

It's the 31st, and only the months above have 31 days.

to clarify what happens, the default value for the 5th parameter is the current day. you pass a number higher than the number of days in that month, it rolls over to the next month.

Outras dicas

You have to pass 5th parameters day in mktime function.

 for($m = 1; $m <= 12; $m++){ 
      $month =  date("F", mktime(0, 0, 0, $m, 1));
      echo $month;
 } 

Day less than 1 (including negative values) reference the days in the previous month.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top