943,597 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3315
  • PHP RSS
Jul 21st, 2007
0

increment time with for looping

Expand Post »
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

PHP Syntax (Toggle Plain Text)
  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. <?
Similar Threads
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Jul 21st, 2007
0

Re: increment time with for looping

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]
Reputation Points: 24
Solved Threads: 0
Junior Poster in Training
assgar is offline Offline
89 posts
since Oct 2006
Jul 24th, 2007
0

Re: increment time with for looping

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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: is it possible to store a query?
Next Thread in PHP Forum Timeline: php is not working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC