Re: Re: A loop for the hours in a working day
| From: | Kelly Hallman | Date: | Sun, 23 Nov 2003 18:49:58 +0000 |
| Subject: | Re: Re: A loop for the hours in a working day | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-170725@lists.php.net to get a copy of this message | ||
On Sun, 23 Nov 2003, Shaun wrote:
> Sorry, worked it out, here it is if anyone needs it :)
> $minutes = array("00", "15", "30", "45");
> $hours = array("07", "08", "09", "10", "11",
> "12", "13", "14", "15", "16",
> "17", "18", "19");
> for ($x = 0; $x < 13; $x++ ) {
> for ($y = 0; $y < 4; $y++ ) {
> echo
> "<tr><td>".$hours[$x].'.'.$minutes[$y]."</td></tr>";
> } }
That works, but it's a little unweildy.. especially if you want to change
the range later (changing the array may not be so bad, but then you've
got to figure out how many iterations your loops must do, etc).
How about something like this:
$hstart = 7; $hend = 19; $interval = 15;
for($h=$hstart; $h < $hend; $h++) {
for($m=0; $m < 60; $m += $interval) {
echo sprintf("%d:%02d",$h,$m); } }
Used sprintf in order to make 0 minutes display as 00. This would be a
great candidate for something to make into a function...
--
Kelly Hallman
// Ultrafancy