Re: [PHP] Month with leading zeros

[email protected] (Jim Lucas)
Newsgroups php.general
Message-ID <[email protected]>
Jim Lucas wrote:
> Ron Piggott wrote:
>> I am wanting to change
>>          echo "<option value=\"" . $months[$month] . "\"";
>>
>> to output the month number, between 01 and 12 --- DATE value m, the
>> month with leading 0's.  How do I do this?  $months is an array, as I
>> have shown below.  Ron
>>
>> <?php
>> $months = array('1' => 'January', '2' => 'February', '3' => 'March', '4'
>> => 'April', '5' => 'May', '6' => 'June', '7' => 'July', '8' => 'August',
>> '9' => 'September', '10' => 'October', '11' => 'November', '12' =>
>> 'December');
>>
>> $current_month = DATE("n");
>>
>> echo "<SELECT NAME=\"order_received_month\">\r\n";
>>
>> foreach (range(1, 12) as $month)
>>      {
>>          echo "<option value=\"" . $months[$month] . "\"";
>>
>> if ( $month == $current_month ) { echo " SELECTED";}
>>
>> echo">" . $months[$month] . "</option>\r\n";
>>      }
>> ?>
>> </select>
>>
>>
> 
> Ok, I will show you two different ways to do this.
> 
> <?php
> 
> $months = array(
>   '1' => 'January',
>   '2' => 'February',
>   '3' => 'March',
>   '4' => 'April',
>   '5' => 'May',
>   '6' => 'June',
>   '7' => 'July',
>   '8' => 'August',
>   '9' => 'September',
>   '10' => 'October',
>   '11' => 'November',
>   '12' => 'December',
> );
> 
> foreach ( $months AS $i => $month ) {
>   $sel = ( $i == date('n') ? ' selected="selected"' : '');
>   echo "<option value='{$month}' {$sel}>{$month}</option>\r\n";
> }
> 
> ?>
> 
> Personally, I would do something like this.
> 
> <?php
> 
> $options = array();
> 
> for ( $i=1; $i<13; $i++ ) {
>   $month = date('F', mktime(0,0,0,$i,2,2000));
>   $sel = ( $i == date('n') ? ' selected="selected"' : '');
>   $options[] = "<option value='{$month}' {$sel}>{$month}</option>";
> }
> 
> $options_list = join("\r\n", $options);
> 
> echo "<select>{$options_list}</select>";
> 
> ?>
> 
> Now, if you are wanting to display the numerical month with leading 
> zero's, then you would want to look at the 'm' option for date() or 
> str_pad().
> 
> 
> something like this might show you what you need.
> 
> <?php
> 
> # str_pad test
> echo str_pad(3, 2, '0', STR_PAD_LEFT)
> 
> ?>
> 
> Hope this helps understand a little more.
> 
> Jim
> 

sorry should have included this in the first one.

http://www.cmsws.com/examples/php/testscripts/[email protected]/0002.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.