increment time with for looping

Thread Solved

Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

increment time with for looping

 
0
  #1
Jul 21st, 2007
Hi
I am not sure I am approaching this correctly.
I need to increment the appointment time by 15 mins for each loop.
This is would be the correct result:
13:00, 13:15, 13:30, 13:45, 14:00,
This is what I am getting:
13:15, 13:15, 13:15, 13:15, 13:15
The user selects the first appointment example 13:00:00 hrs (HH:MM)
and one or more time blocks. Time blocks range from 1 to 10. Each is 0.15 mins (H).
$event_time = 13:00;//start time
$time_block = 5;// number of loops
$event_length = 0.15;//start time increment by

  1. <?php
  2. /**--------------------------insert appointment in database--------------------------**/
  3. //loop the number of time blocks
  4. for($i = 0; $i < $time_block; $i++)
  5. {
  6.  
  7.  
  8. /**---------------------calculate total duration---------------------**/
  9. //Note: event time length * time_block = total event duration
  10. //split event time
  11. list($event_hour, $event_min) = explode(".",$event_time);
  12.  
  13. //event length
  14. list($length_hour, $length_min) = explode(".",$event_length);
  15.  
  16. //convert event time to minutes
  17. $event_minutes = ($event_hour * 60) + $event_min;
  18.  
  19. //convert event time length to minutes
  20. $length_minutes = (length_hour * 60) + $length_min;
  21.  
  22. //add event time to event duration = next event time
  23. $total_min = $event_minutes + $length_minutes++;
  24.  
  25. //convert minutes to hours and minutes
  26. if($total_min < 0)
  27. {
  28. $min =Abs($total_min);
  29. }
  30. else
  31. {
  32. $min = $total_min;
  33. }
  34. $H = Floor($min/60);
  35. $M = ($min - ($H * 60))/100;
  36. $hours = $H + $M;
  37.  
  38. if($total_min < 0)
  39. {
  40. $hours = $hours * (-1);
  41. }
  42.  
  43. $expl = explode(".",$hours);
  44. $H = $expl[0];
  45.  
  46. if(empty($expl[1]))
  47. {
  48. $expl[1] = 00;
  49. }
  50.  
  51. $M = $expl[1];
  52. if(strlen($M) < 2)
  53. {
  54. $M = $M . 0;
  55. }
  56.  
  57. $new_event_time = $H.".".$M;//total duration
  58.  
  59.  
  60.  
  61. /**-------------------database insert statement goes here ------------------**/
  62.  
  63. }
  64. <?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 82
Reputation: assgar is an unknown quantity at this point 
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Re: increment time with for looping

 
0
  #2
Jul 21st, 2007
Hi
Thanks for the response.
This is the solution:

[PHP]
$event_time = "13:00";
$time_block = 5;
$event_length = 15; // minutes
for($i = 0, $eTime = strtotime($event_time);
$i < $time_block;
$i++, $eTime = strtotime("+$event_length minutes", $eTime))
{
$new_event_time = date('H:i', $eTime);
echo "<p>$new_event_time</p>\n";
}
[/PHP]
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: increment time with for looping

 
0
  #3
Jul 24th, 2007
aasgar. Your problem is when you do an explode() on the $even_time your seperator is "." but there are no dots in 13:00 it should be
exlode(":", $event_time);

Fix that, and it should work for you.

Cheers.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC