|
 |
|
 |
02-16-2006, 07:01 AM
|
#1 (permalink)
|
|
Recruit
Join Date: May 2005
Posts: 13
|
Easy (for you ninjas) loop question
Hey guys,
really dumb question that I know has a simple answer...
I have this loop that generates a dropdown list for years. The problem is that the first current year it grabs initially is great (06) after it is incremented, I loose my "0" before the 6.. i.e...
Returns: 06,7,8,9
Instead of: 06,07,08,09
I'm sure there's a function or something that preserves the double, but can't seem to find it.
Here's the loop
Code:
<select name="expire_year">
<?
$curYear = date("y");
$yearCount = 1;
echo "<option value='' SELECTED>- Select -</option>\n";
while ($yearCount < 10) {
echo "<option value='".$curYear."'>".$curYear."</option>\n";
$yearCount ++;
$curYear ++;
}
?>
</select>
|
|
|
02-16-2006, 07:19 AM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
personally I would use date("Y") so you have the 4 digit year. since they don't start with "0" you won't have this problem.
you could also create a string array with predefined dates:
PHP Code:
<? $years = array("06","07","08","09","10"); foreach ($years as $year) { echo "<option value='".$year."'>".$year."</option>\n"; } ?>
or str_pad if you want to keep using your way
PHP Code:
<? $year = date("y");
while($year < 10) { $year = str_pad($year, 2, "0", STR_PAD_LEFT);
echo "<option value='".$year."'>".$year."</option>\n";
$year++; } ?>
hth 
|
|
|
02-16-2006, 07:24 AM
|
#3 (permalink)
|
|
Recruit
Join Date: May 2005
Posts: 13
|
The reason i have to keep it like that is because it's getting passed to Authorize.net..
they require the expiration date to be sent in "mmyy".
So, i have 2 drop downs, one for month and one for year.
Then when I process it, i combine them.
$expire_date = $month.$year;
But it doesn't accept the expire date if for example it got May 08 as "508". It would have to be 0508.
I'm writing this loop so it isn't hard-coded and I don't have to update it next year. make sense ??
Thanks for the input!!
|
|
|
02-16-2006, 07:42 AM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
yes makes sense. you might want to set your end year dynamically too then..
$end_year = $cur_year + 6;
then you won't have to update your end year next year as well. 
|
|
|
02-16-2006, 02:55 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Feb 2006
Posts: 1
|
Try this...
What about just appending the 0 as text when necessary? (Also, no need to increment a counter, just set an end point)
Code:
$curYear = floor(date("y"));
$endYear = $curYear + 10;
echo "<option value='' SELECTED>- Select -</option>\n";
while ($curYear < $endYear) {
if ($curYear < 10) {
$displayYear = "0".$curYear;
} else {
$displayYear = $curYear;
}
echo "<option value='".$displayYear."'>".$displayYear."</option>\n";
$curYear ++;
}
|
|
|
02-16-2006, 05:12 PM
|
#6 (permalink)
|
|
Recruit
Join Date: May 2005
Posts: 13
|
Very nice, nudave04.. I like that option a lot.
simple, not overthought. great.
And I can understand it
Thanks guys! you all rulzor !
|
|
|
02-16-2006, 05:24 PM
|
#7 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
That's cool that it all makes sense. Another cool thing is that
PHP Code:
$displayYear = str_pad($year, 2, "0", STR_PAD_LEFT);
is equivalent to
PHP Code:
if ($curYear < 10) { $displayYear = "0".$curYear; } else { $displayYear = $curYear; }
So now you can plug that into nudave04's loop and get
PHP Code:
while ($curYear < $endYear) { $displayYear = str_pad($curYear, 2, "0", STR_PAD_LEFT); echo "<option value='".$displayYear."'>".$displayYear."</option>\n"; $curYear ++; }
__________________
Stop intellectual property from infringing on me
|
|
|
02-16-2006, 06:43 PM
|
#8 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 676
|
or
PHP Code:
echo "<option value='' selected='selected'>- Select -</option>\n";
$curYear = (int) date('y'); for ($i=0; $i < 10; ++$i) { $displayYear = str_pad($curYear+$i, 2, '0', STR_PAD_LEFT); echo "<option value='$displayYear'>$displayYear</option>\n"; }
|
|
|
02-16-2006, 07:05 PM
|
#9 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,489
|
i think i'll write a book .. 101 ways to code a year drop down in PHP 
|
|
|
02-16-2006, 10:48 PM
|
#10 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
|
Or more simplyfied:
PHP Code:
echo "<option value='' SELECTED>- Select -</option>\n";
for ($i=floor(date("y")), $j=$i+10; $i < $j; ++$i)
printf("<option value='%.2d'>%.2d</option>\n", $i, $i);
But then again there are hundreds of ways you can form something like this.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:28 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|