Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/53199-solved-subtracting-time/
Share on other sites

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?

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;
?>

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);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.