tekrscom Posted May 27, 2007 Share Posted May 27, 2007 I have spent the last few hours trying to find a way to subtract two times, none of the information that I find seems relevant to what I am trying to do... A technician is filling out a form, posting the time he logs in and the time he logs out, both of which are being recorded in the database. But prior to the UPDATE, I need to calculate how many hours he was there, e.g. 2.5 hours, then post that also ... then multiply that times his hourly rate and post that also, which ultimately gives the times he was there, how many hours total and how much his time cost against the work, but I think I'm getting off subject... here's my latest failure for code. if($_POST[logged_in_time_am_pm] == "pm" && $_POST[logged_in_time_hours] != "12"){$i = "12";}elseif($_POST[logged_in_time_am_pm] == "am"){$i = "0";} $logged_in_time_hours = $_POST[logged_in_time_hours] + $i; $logged_in_time = date('H:i:s', strtotime("$logged_in_time_hours:$_POST[logged_in_time_minutes]:00")); if($_POST[logged_out_time_am_pm] == "pm" && $_POST[logged_out_time_hours] != "12"){$i = "12";}elseif($_POST[logged_out_time_am_pm] == "am"){$i = "0";} $logged_out_time_hours = $_POST[logged_out_time_hours] + $i; $logged_out_time = date('H:i:s', strtotime("$logged_out_time_hours:$_POST[logged_out_time_minutes]:00")); $tech_1_hours = timediff($logged_in_time,$logged_out_time); Thanks for any help anyone can give me. Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/ Share on other sites More sharing options...
gabeg Posted May 27, 2007 Share Posted May 27, 2007 If you use 24hr clock instead of 12hr it might help.. ie... logins at 11:00, logs out at 13:30=> 13 - 11 = 2, 30-00 = 30.. thats 2 hours 30 mins logged in to multiply times wage, change the mins to decmimal, so 30/60 = .5... then do 2.5*wage does this help? Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/#findComment-262843 Share on other sites More sharing options...
tekrscom Posted May 27, 2007 Author Share Posted May 27, 2007 Actually I have tried that and it doesn't quite work out right, because of when there are minutes then you have to add an hour... then you'd have to covert the minutes into a decimal and then join the hours and decimal minutes into a single decimal number... Surely there is a simple PHP function for this, but I have been up and down the php.net functions and can't seem to find one... Does anyone know of a way to do this? Subtract login time, e.g. 13:00:00, from logout time, e.g. 15:30:00, into a decimal number, e.g. 2.50 hours? Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/#findComment-262901 Share on other sites More sharing options...
gabeg Posted May 27, 2007 Share Posted May 27, 2007 I dont get what you mean about adding an hour if there are minutes? It may sound complicated at first but it really isn't example: <?php $hour1 = "13:00"; $hour2 = "15:30"; $array1 = explode(":",$hour1); $array2 = explode(":",$hour2); $hourdiff = $array2[0] - $array1[0]; //should give you .5....(30-0)/60 $min diff = ($array2[1] - $array2[1]) / 60; $total = $hourdiff"."$mindiff; $wage = "6.25"; $totalpay = (double)$total * $wage; echo $totalpay; ?> Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/#findComment-262905 Share on other sites More sharing options...
tekrscom Posted May 27, 2007 Author Share Posted May 27, 2007 With that formula, it comes up with 5 hours and 0 minutes when I submit logout time of 18:00 and login time of 13:30 Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/#findComment-262928 Share on other sites More sharing options...
tekrscom Posted May 28, 2007 Author Share Posted May 28, 2007 Ok, I dug a little deeper in the vast depths of phpfreaks forums and found a solution... and here it is in case anyone else needs it... $log_in_time_string = strtotime($logged_in_time); $log_out_time_string = strtotime($logged_out_time); $difference_in_seconds = ($log_out_time_string - $log_in_time_string); $total_time = ($difference_in_seconds / 3600); Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/#findComment-262976 Share on other sites More sharing options...
gabeg Posted May 28, 2007 Share Posted May 28, 2007 With that formula, it comes up with 5 hours and 0 minutes when I submit logout time of 18:00 and login time of 13:30 cause of the typo $min diff instead of $mindiff.. glad you got it solved though. always like seeing thing's differently Quote Link to comment https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/#findComment-263091 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.