User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 392,007 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,177 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 855 | Replies: 2 | Solved
Reply
Join Date: Oct 2006
Posts: 71
Reputation: assgar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Help increment time with for looping

  #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

<?php
/**--------------------------insert appointment in database--------------------------**/
//loop the number of time blocks
for($i = 0; $i < $time_block; $i++)
 {
 
 
           /**---------------------calculate total duration---------------------**/
     //Note: event time length * time_block = total event duration
    //split event time
     list($event_hour, $event_min) = explode(".",$event_time);
 
    //event length
    list($length_hour, $length_min) = explode(".",$event_length);
 
    //convert event time to minutes
     $event_minutes = ($event_hour * 60) + $event_min;
 
     //convert event time length to minutes
     $length_minutes = (length_hour * 60) + $length_min;
 
    //add event time to event duration = next event time
    $total_min = $event_minutes + $length_minutes++;
 
   //convert minutes to hours and minutes
   if($total_min < 0)
         {
             $min =Abs($total_min);
  }
  else
      {
   $min = $total_min;
     }
   $H = Floor($min/60);
   $M = ($min - ($H * 60))/100;
   $hours = $H + $M;
 
   if($total_min < 0) 
   {
      $hours = $hours * (-1);
  }
 
  $expl = explode(".",$hours);
  $H = $expl[0];
 
  if(empty($expl[1])) 
     {
   $expl[1] = 00;
     } 
 
  $M = $expl[1];
  if(strlen($M) < 2)
     {
  $M = $M . 0;
     }
 
     $new_event_time = $H.".".$M;//total duration
 
 
 
 /**-------------------database insert statement goes here ------------------**/
 
   }
<?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 71
Reputation: assgar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Solution Re: increment time with for looping

  #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  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: increment time with for looping

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 10:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC