Jump to content

Birthdate


timecatcher

Recommended Posts

Ok im in a competition for the best register.php script, both secure customizable and bug free. So you may see a few more of these posts, sorry guys. :) You have a GOOD reputation of providing the answers I need 99% of the time when im having a blond moment.

 

So heres my question, Is there any easier way of listing all the years since 1900-2009 without typing and c&p. (Oh, and if so what is it, im sure its plainly simple as it seems like it is in my head but I just can't think of a way.)

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/167386-birthdate/
Share on other sites

for a more dynamic approach (just playing on Crayon Violent's script):

<?php
$this_year = date("Y");
$last_year = $this_year - 1;
echo "<select name='year'>\n";
echo "\t<option value='$this_year' selected='selected'>$this_year</option>\n";
for ($x = $last_year;$x>=1900; $x--) {
  echo "\t<option value='$x'>$x</option>\n";
}
echo "</select>\n";

*edit

remembered that most people have the most current year first. fixed the code to reflect that.

Link to comment
https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882683
Share on other sites

well there isn't a foreach in any of the code examples posted.  But anyways, a foreach is actually pretty simple: You have an array and for each value (element) in the array, you execute the code.

 

$array = array('a','b','c');
foreach($array as $value) {
  // execute code here
}

 

So in that loop, $value represents the current element value.  So in the first iteration, $value == 'a', 2nd iteration, $value == 'b', etc...  if you need to know the element's key, you can do this:

 

$array = array('a','b','c');
foreach($array as $key => $value) {
  // execute code here
}

 

so on first iteration, $key == 0, 2nd iteration, $key == 1, etc...

 

You can read this tutorial for more info about foreach loops (and other loops in general):

 

http://www.phpfreaks.com/tutorial/php-loops

Link to comment
https://forums.phpfreaks.com/topic/167386-birthdate/#findComment-882719
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.