How do I create for loop for an array (month day year.....)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
redrocket1855
Forum Newbie
Posts: 8
Joined: Tue Sep 15, 2009 12:30 am

How do I create for loop for an array (month day year.....)

Post by redrocket1855 »

I was wondering how to create a loop with in an array keeping my code more concise ....

Code: Select all

 
[color=#BF0000]$days = array(1 => 1, 2, 3, ... , 30, 31);[/color]
$years = array(2009 => 2009, 2010, 2011, ...);
$hours = array(1 => 1, 2, 3, ... , 23, 24);
$minutes = array(0 => '00', '01', '02', ... , '58', '59');
I know this code for the days will display the numbers 1-31. I do not understand how to put this in to the array of $days..

Code: Select all

<?php
$i=1;
while($i<=31)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?>
Thanks I am new to PHP and appreciate all the feedback..
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: How do I create for loop for an array (month day year.....)

Post by pbs »

Below is code to display drop down box for year, month and day.

Code: Select all

 
<select name="dobyy">
    <option value="">yyyy</option>
    <?php
        for($y=(date("Y"));$y>(date("Y")-10);$y--)
        {
    ?>
            <option value="<?=$y?>" <?php if (trim($_REQUEST['dobyy']) == $y) { echo " selected "; }?> ><?=$y?></option>
    <?php 
        }
    ?>
</select>
<select name="dobmm">
    <option value="">mm</option>
    <?php
        for($m=1;$m<13;$m++)
        {
    ?>
            <option value="<?=str_pad($m, 2, "0", STR_PAD_LEFT)?>" <?php if (trim($_REQUEST['dobmm']) == str_pad($m, 2, "0", STR_PAD_LEFT)) { echo " selected "; }?>  ><?=str_pad($m, 2, "0", STR_PAD_LEFT)?></option>
    <?php 
        }
    ?>
</select>
<select name="dobdd">
    <option value="">dd</option>
    <?php
        for($d=1;$d<32;$d++)
        {
    ?>
            <option value="<?=str_pad($d, 2, "0", STR_PAD_LEFT)?>" <?php if (trim($_REQUEST['dobdd']) == str_pad($d, 2, "0", STR_PAD_LEFT)) { echo " selected "; }?> ><?=str_pad($d, 2, "0", STR_PAD_LEFT)?></option>
    <?php 
        }
    ?>
</select>   
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I create for loop for an array (month day year.....)

Post by Christopher »

redrocket1855 wrote:I know this code for the days will display the numbers 1-31. I do not understand how to put this in to the array of $days..

Code: Select all

$days = array();
$i=1;
while($i<=31)
  {
  $days[] = $i;
  $i++;
  }
Although if you checked the manual for array functions, you would have found this:

Code: Select all

$days = range(1, 31);
(#10850)
Post Reply