nerrad Posted July 20, 2007 Share Posted July 20, 2007 Hi people im trying to get the difference between two time's and then subtract another time from that total here is what i have found so far: function get_time_difference( $start, $end, $lunch ) { $uts['start'] = strtotime( $start ); $uts['end'] = strtotime( $end ); $uts['lunch'] = strtotime ( $lunch ); if( $uts['start']!==-1 && $uts['end']!==-1 ) { if( $uts['end'] >= $uts['start'] ) { $diff = $uts['end'] - $uts['start']; $diff = $diff - $uts['lunch'];//take away lunch if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400; if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600; if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60; $diff = intval( $diff ); return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) ); } else { trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING ); } } else { trigger_error( "Invalid date/time data detected", E_USER_WARNING ); } return( false ); } im trying to use that to calc the total time and then to subtract the lunchbreak and this to store it into a variable and show elsewhere later. $start = $info['event_time'];//start time $end = $info['event_end_time'];//finish time $lunch = $info['lunch'];//lunch time (in minutes) if( $tDiff=@get_time_difference($start, $end, $lunch) ) { $tTotal = "Hours: " . sprintf( '%02d:%02d', $diff['hours'], $diff['minutes'] ); } else { echo "Hours: Error"; } All i am getting is 00:00 what am i doing wrong? Thanks for the help Quote Link to comment Share on other sites More sharing options...
gerkintrigg Posted July 20, 2007 Share Posted July 20, 2007 hmm - how is it being stored in the database? could you echo each variable at various points through the script and see where the error occurs... go through it systematically and check that it works at certain points... it should hint to where the error is. Quote Link to comment Share on other sites More sharing options...
nerrad Posted July 23, 2007 Author Share Posted July 23, 2007 the times are stored in the database as varchars and in the HH:MM format, i have debugged the code and i have the following: $uts['start'] = 1185172200 $uts['end'] = 1185208200 $uts['lunch'] = 1185146100 when i substract the end from the start the difference is: $diff = 36000 which im assuming is 10 hours in seconds. the problem now comes when i subtract the lunch from the $diff, I am now getting -14hours-15minutes. What have i done wrong? Are they stored in the database wrong? Quote Link to comment Share on other sites More sharing options...
Guardian-Mage Posted July 23, 2007 Share Posted July 23, 2007 Are you doing it in the right order? Lunch - diff not the other way around? Quote Link to comment Share on other sites More sharing options...
nerrad Posted July 23, 2007 Author Share Posted July 23, 2007 Getting closer, that makes the total hours 09:26 but that's not correct it should be 9:45!! Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 23, 2007 Share Posted July 23, 2007 are there punches in and out for lunch or is it just a flat amount of time? Quote Link to comment Share on other sites More sharing options...
ViN86 Posted July 23, 2007 Share Posted July 23, 2007 you said youre storing it in HH:MM format? i would use the time() function (in PHP) and store that into the database in an INT field. then i would just subtract the values and write my own function to convert seconds to an HH:MM:SS format. Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 23, 2007 Share Posted July 23, 2007 here is the classes.inc i use for my timeclock i wrote for work. might help a little. <?php class HoursDay { var $dte = ''; var $row = ''; var $rowS =''; var $row_S2 =''; var $hours = 0; var $total = 0; var $punches; var $cnt = 0; var $f_name = ''; var $l_name = ''; var $eid = ''; var $in = '<td> '; var $out ='<td> '; var $row_hrs = '<td> '; ////////////////////////////// function numHours($time_in, $time_out) { $cnt=$this->cnt; if ($time_out != "0") { $this->hours = round((($time_out - $time_in) / 60),2); } $this->total += $this->hours; $this->punches[$cnt][in] = $this->in; $this->punches[$cnt][out] = $this->out; $this->cnt += 1; } //////////////////////////// function rowStart($string) { $this->rowS .= $string; } /////////////////////////// function rowInfo($string) { $this->rowS2 = $string; } /////////////////////////// function rowAdd($string) { $this->row .= $string; } } /////////////////////// function timeToMin($time) { $time_ = explode(":", $time); $time_min = ($time_[0] * 60 )+$time_[1]; return($time_min); } //////////////// function writeToClass($n, &$day, $date, $time_in, $time_out, $fname, $lname) { $day->dte = date("l", strtotime($date)).", ".date("M-d-y",strtotime($date)); $day->rowInfo("<td>{$fname}</td><td>{$lname}</td><td>".date("l", strtotime($date)).", ".date("M-d-y",strtotime($date))."</td>"); $day->numHours(timeToMin($time_in), timeToMin($time_out)); $day->in .= "{$time_in}<br /><hr/>"; $day->out .= "{$time_out}<br /><hr/>"; $day->row_hrs .= "{$day->hours}<br /><hr/>"; } //////////////// ?> the writeToClass function is just to help with outputting 14 days worth of time punches into something readable. Quote Link to comment Share on other sites More sharing options...
nerrad Posted July 23, 2007 Author Share Posted July 23, 2007 Thanks for the help guys, but i would prefer to use this i have spent ages and ages trying to figure out what is wrong! I'm willing to change it if there is no chance of "fixing" what wrong but surely the problem lies in this part of the code? if( $uts['end'] >= $uts['start'] ) { $diff = $uts['end'] - $uts['start']; $diff = $uts['lunch'] - $diff;//take away lunch if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400; if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600; if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60; $diff = intval( $diff ); return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) ); My understanding of it is that it subtracts the end hours from the start hours to get the difference and it store them in the unix time format? So surely there is only a little something i need to change here $diff = $uts['lunch'] - $diff;//take away lunch Help is greatly appreciated, Also please dont reply with nothing to technical i have only been learning this for less then a week! Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 23, 2007 Share Posted July 23, 2007 ahh ok I see where the problem lies, you are trying to subtract the total hours for the day from the time for lunch. $diff = $uts['end'] - $uts['start']; $diff = $uts['lunch'] - $diff;//take away lunch #Right here you are subtracting the hours for the day from the time they went to lunch. this will give you a time not number of hours. what we have to figure out how long the lunch was first, so you either need to set a hard coded standard lunch break time or have a in and out punch of lunch. so you can get the number of minutes they were on lunch. So if Lunch is always 1 hour lets say. then $uts['lunch'] = "3600"; //1 hour in seconds $diff = $uts['end'] - $uts['start']; //sets number hours for whole day. $diff -= $uts['lunch'] ; //take away lunch hour Edited :typos / grammer Quote Link to comment Share on other sites More sharing options...
nerrad Posted July 23, 2007 Author Share Posted July 23, 2007 Hi grimmier, There is not actually a time they went to lunch, the user selects how long they had for lunch which are in 5 minute increments 0,5,10,15,20,25,30,35,40,45,50,55,60. And are stored in the database as a varchar in the format HH:MM e.g. 00:15; if i was to store the results i get from the database in single variables instead of an array they would look like this: $start = '07:30'; // time they started work $end = '17:30'; // time they finished work $lunch = '00:15'; // amount of time they had for lunch (15 minutes) So i guess what i need to do if figure out how to convert the 00:15 thats stored in the database to a format i can use to subtract from the total worked time. But i cant do that!! *BANGS HEAD AGAINST A BRICK WALL* BTW i do get the correct amount of hours if i remove the "take away lunch coding" Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 23, 2007 Share Posted July 23, 2007 ok i got it now try this $uts['lunch'] = $uts['lunch'] * 60; // this converts the amount of time for lunch into minutes, not a timestamp. then you can use this down below $diff = $uts['end'] - $uts['start']; $diff -= $uts['lunch'] ; //take away lunch Quote Link to comment Share on other sites More sharing options...
nerrad Posted July 23, 2007 Author Share Posted July 23, 2007 nope that dont work getting i'm 0 minutes, Ahhh this is so frustrating!! I Put together a bit of code for debugging purposes if you want to take a look? <?php $start = "07:30"; $end = "17:30"; $lunch = "00:15"; $uts['start'] = strtotime( $start ); $uts['end'] = strtotime( $end ); $uts['lunch'] = strtotime( $lunch ); $uts['lunch'] = $uts['lunch'] * 60; // this converts the amount of time for lunch into minutes, not a timestamp. $diff = $uts['end'] - $uts['start']; echo $diff . ' - After end - start<BR>'; $diff = $uts['lunch'] - $diff; //take away lunch // GIVES NEGATIVE RESULTS so changed to ^ $diff -= $uts['lunch'] ; //take away lunch echo $diff . ' - After lunch - diff<BR>'; if( $days=intval((floor($diff/86400))) ) echo $days . ' - days after intval floor$diff/86400<BR>'; $diff = $diff % 86400; echo $diff . ' - diff after $diff = $diff % 86400<BR>'; if( $hours=intval((floor($diff/3600))) ) echo $hours . ' - hours after intval floor$diff/3600<BR>'; $diff = $diff % 3600; echo $diff . ' - diff after $diff = $diff % 3600<BR>'; if( $minutes=intval((floor($diff/60))) ) echo $minutes . ' - minutes after intval floor$diff/60<BR>'; $diff = $diff % 60; echo $diff . ' - diff after $diff = $diff % 60<BR>'; $diff = intval( $diff ); echo $diff . ' - diff after $diff = intval( $diff )<BR>'; echo '<BR><BR><BR>all vars <BR><BR><BR>'; echo $start . '- START as string<BR>'; echo $end . '-END as string<BR>'; echo $lunch . '-LUNCH as string<BR>'; echo 'Arrays:<BR>'; echo $uts['start'] . '-converted start (strtotime)<BR>'; echo $uts['end'] . '-converted end (strtotime)<BR>'; echo $uts['lunch'] . '-converted lunch (strtotime)<BR>'; echo 'Variables:<BR>'; echo $days . '-Days<BR>'; echo $diff . '- Diff<BR>'; echo $hours . '-Hours<BR>'; echo $minutes . '-Minutes<BR>'; echo '<B>TOTAL TIME</B><BR>'; echo $hours . ':' . $minutes; ?> Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 23, 2007 Share Posted July 23, 2007 Ok try this, its he whole function function get_time_difference( $start, $end, $lunch ) { $lunch_time = explode(":", $lunch); //removes the : from the hh:mm format into hours and min. $uts['start'] = strtotime( $start ); $uts['end'] = strtotime( $end ); $uts['lunch'] = ($lunch_time[0] * 3600) + ($lunch_time[1] * 60); //convert both hours and min to seconds and add them together. if( $uts['start']!==-1 && $uts['end']!==-1 ) { if( $uts['end'] >= $uts['start'] ) { $diff = $uts['end'] - $uts['start']; $diff -= $uts['lunch']; // take away lunch. if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400; if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600; if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60; $diff = intval( $diff ); return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) ); } else { trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING ); } } else { trigger_error( "Invalid date/time data detected", E_USER_WARNING ); } return( false ); } Quote Link to comment Share on other sites More sharing options...
nerrad Posted July 23, 2007 Author Share Posted July 23, 2007 grimmier you are truely a legend!! Thank you very much for your help it is really appreciated.. Keep up the good work mate... Admins give this person another star!!! He Deserves it! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.