increment time with for looping

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
assgar
Forum Commoner
Posts: 46
Joined: Fri Apr 20, 2007 9:00 pm

increment time with for looping

Post by assgar »

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


Code: Select all

<?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 ------------------**/
				
   }
<?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

it would be much more sensible to convert your time into a timestamp using strtotime(), and finally formatting it using date()
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Yes, by all means, take advantage of the excellent time value functionality built into PHP. You should be able to determine the total duration of an appointment with perhaps 3 or 4 lines of script.

Here's a resource that might help you: http://www.phpbuilder.com/columns/akent20000610.php3
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

strtotime has become my favorite php function.

Code: Select all

date('m/d/y H:i:s', strtotime('+15 minutes', strtotime($date));
Post Reply