Jump to content

timestamps differences


ramiwahdan

Recommended Posts

Hi,

I have 2 db fields with timestamp datatype (ClockingInDate and ClockingOutDate) and i am trying to get the difference between them then update the new db called duration with float datatype field.

PHP:

if (isset($_POST["clockout"])){
		
		$result3=mysqli_query($con, "select * from attendance_records where OracleID='$session_id'")or die('Error In Session');
		$row3=mysqli_fetch_array($result3);
		
		$end_date =  $row3['ClockingOutDate'];
		$startdate = $row3['ClockingInDate'];
		$diff = strtotime($end_date) - strtotime($startdate);
		$fullDays    = floor($diff/(60*60*24));
		$fullHours   = floor(($diff-($fullDays*60*60*24))/(60*60));   
		$fullMinutes = floor(($diff-($fullDays*60*60*24)-($fullHours*60*60))/60);  

		$duration = $fullMinutes;
		$query3=mysqli_query($con, "update attendance_records set Duration = '$duration' where OracleID='$session_id' and isdone='$isdone'")or die('Error In Session');
		header('location:index.php');
		
	}

 

* come to think of that again, i always get zero, is it because it never reaches days in my program! I am using this for attendance system so only hours and minutes are used. Please help.

Edited by ramiwahdan
thoughts
Link to comment
Share on other sites

mysql> select login_time
    ->      , logout_time
    ->      , timediff(logout_time, login_time) as diff
    -> FROM login;
+---------------------+---------------------+----------+
| login_time          | logout_time         | diff     |
+---------------------+---------------------+----------+
| 2020-03-12 12:30:00 | 2020-03-13 15:02:30 | 26:32:30 |
+---------------------+---------------------+----------+

 

Link to comment
Share on other sites

10 minutes ago, Barand said:

mysql> select login_time
    ->      , logout_time
    ->      , timediff(logout_time, login_time) as diff
    -> FROM login;
+---------------------+---------------------+----------+
| login_time          | logout_time         | diff     |
+---------------------+---------------------+----------+
| 2020-03-12 12:30:00 | 2020-03-13 15:02:30 | 26:32:30 |
+---------------------+---------------------+----------+

 

Thanks, I tried it but still no results.

code:

$result3=mysqli_query($con, "select ClockingInDate, ClockingOutDate, timediff(ClockingOutDate,ClockingInDate) as diff from attendance_records where OracleID='$session_id'")or die('Error In Session');
		$row3=mysqli_fetch_array($result3);

		$duration = $row3['diff'];
		$query3=mysqli_query($con, "update attendance_records set Duration = '$duration' where OracleID='$session_id' and isdone='$isdone'")or die('Error In Session');

 

Link to comment
Share on other sites

Why are you even attempting to store that duration. You can get it any time you need it with a query. 

Rule of DB design - don't store derived data.

If you really insist on storing it, why do need two queries?

UPDATE attendance_records
SET duration = timediff(...)
WHERE ...

- a single update would do the job

  • Great Answer 2
Link to comment
Share on other sites

2 hours ago, Barand said:

Why are you even attempting to store that duration. You can get it any time you need it with a query. 

Rule of DB design - don't store derived data.

If you really insist on storing it, why do need two queries?


UPDATE attendance_records
SET duration = timediff(...)
WHERE ...

- a single update would do the job

I tried to do that if less than minute its fine but i tried 1 minute or more i get wrong result. Attached is a screenshot of 1 minute but giving result of 100 instead of 60 in seconds 

screenshot.png

Link to comment
Share on other sites

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.