i\'m trying to make a loop that will output this:
08:00
08:15
08:30
08:45
09:00
09:15
09:30
09:45
i need it to go from 08:00 to 17:00
DateInterval
can be used to create a new dateinterval
object for our date calculation and uses in any script. In the advance PHP object oriented style for all date & time calculations this format is useful.
Check below links:
http://php.net/manual/en/class.dateinterval.php
http://www.plus2net.com/php_tutorial/date-interval.php
my simple logic here
$start=strtotime('00:00');
$end=strtotime('23:30');
for ($i=$start;$i<=$end;$i = $i + 15*60)
{
//write your if conditions and implement your logic here
echo date('g:i A',$i).'<br>';
}
in loop you can play what you want
You need to move the last line outside of the outer for loop.
for ($i = 8; $i <= 16; $i++){
for ($j = 0; $j <= 45; $j+=15){
//inside the inner loop
echo_datelist($i, $j, $day, $month, $year);
}
//inside the outer loop
}
//outside the outer loop
echo_datelist(17, 0, $day, $month, $year);
In plain terms, you are saying:
For each hour between 8 and 16
For each 15 minute interval
Echo the time
End
Echo 17:00
End
Instead of:
For each hour between 8 and 16
For each 15 minute interval
Echo the time
End
End
Echo 17:00
I would consider performing your sql query for all hours of the day and then picking out the ones within the time from, otherwise you be doing an sql query for each 15 minute interval (37 queries with your sample data)
With PHP 5 >= 5.3.0 you can use DateTime::add
, see: http://www.php.net/manual/de/datetime.add.php
i was working on a similar problem, but with the start/end times being changeable.
this may need a little refinement, but i can't break it.
all you need to supply in the beginning are the date and times.
$day = "10/14/2011";
$startTime = date(strtotime($day." 16:00"));
$endTime = date(strtotime($day." 19:15"));
$timeDiff = round(($endTime - $startTime)/60/60);
$startHour = date("G", $startTime);
$endHour = $startHour + $timeDiff;
for ($i=$startHour; $i <= $endHour; $i++)
{
for ($j = 0; $j <= 45; $j+=15)
{
$time = $i.":".str_pad($j, 2, '0', STR_PAD_LEFT);
echo (date(strtotime($day." ".$time)) <= $endTime) ? date("g:i", strtotime($day." ".$time))."<br>" : "";
}
}
outputs:
4:00 4:15 4:30 4:45 5:00 5:15 5:30 5:45 6:00 6:15 6:30 6:45 7:00 7:15
Looks unnecessarily complicated to me. The following will print out what you want. Presumably it can be adapted for use in your code. Sorry about the messy end-condition.
$min=array("00","15","30","45");
for($i=8;$i<17;$i++)
foreach ($min as $v)
print "$i:$v\n";
print "17:00\n";
Or, if you want to do this in a slightly more opaque way...
for($i=8*60;$i<=17*60;$i+=15)
print floor($i/60) . ":" . ($i/60-floor($i/60))*60 . "\n";
The above calculates a minutes value for 8 o'clock and then adds fifteen minutes repeatedly. You then use some math to extract hours and minutes from the running variable.