PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Month with leading zeros
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Month with leading zeros

Réponse
 
LinkBack Outils de la discussion
Vieux 10/05/2008, 21h56   #1
Ron Piggott
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Month with leading zeros

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>

  Réponse avec citation
Vieux 10/05/2008, 22h48   #2
David Otton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Month with leading zeros

2008/5/10 Ron Piggott <ron.php@actsministries.org>:
> 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>


Try this (from memory, untested).

for ($i = 1; $i >= 12; $i++)
{
$month = date( 'm', strtotime( "1970-$i-01" ));
echo "<option value=\"$i\">$month</option>";
}

Or this:

printf( "%02d", 5 );

However, please consider replacing numeric identifiers for months with
textual ones ('M' rather than 'm'). [01] [Apr] [2008] doesn't cause
the confusion that [01] [04] [2008] does.
  Réponse avec citation
Vieux 10/05/2008, 22h53   #3
David Otton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Month with leading zeros

> for ($i = 1; $i >= 12; $i++)

I'm an idiot.

for ($i = 1; $i <= 12; $i++)
  Réponse avec citation
Vieux 11/05/2008, 08h28   #4
Casey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Month with leading zeros

On 5/10/08, Ron Piggott <ron.php@actsministries.org> 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 ($months as $i => $month)
> {
> echo "<option value=\"" . $month . "\"";
>
> if ( $i == $current_month ) { echo " SELECTED";}
>
> echo">" . $month . "</option>\r\n";
> }
> ?>
> </select>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
-Casey
  Réponse avec citation
Vieux 11/05/2008, 09h06   #5
CJ Willcock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Month with leading zeros

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>
>


You may update like:

foreach(range(1, 12) as $key => $month) {
echo "<option value=\"" . str_pad( $key, 2, "0", STR_PAD_LEFT ) . "\"";
}


Cheers,
CJW
  Réponse avec citation
Vieux 11/05/2008, 09h08   #6
CJ Willcock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Month with leading zeros

CJ Willcock 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>
>>

>
> You may update like:
>
> foreach(range(1, 12) as $key => $month) {
> echo "<option value=\"" . str_pad( $key, 2, "0", STR_PAD_LEFT ) . "\"";
> }
>
>
> Cheers,
> CJW



Sorry, forgot to increment the key. Read:

echo "<option value=\"" . str_pad( $key + 1, 2, "0", STR_PAD_LEFT )
.. "\"";


Cheers,
CJW
  Réponse avec citation
Vieux 11/05/2008, 09h45   #7
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Month with leading zeros

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 s understand a little more.

Jim
  Réponse avec citation
Vieux 11/05/2008, 09h47   #8
Jim Lucas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Month with leading zeros

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 s understand a little more.
>
> Jim
>


sorry should have included this in the first one.

http://www.cmsws.com/examples/php/te...s.org/0002.php
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18820 seconds with 16 queries