Jump to content
#FlattenTheCurve ×
ramiwahdan

timestamps differences

Recommended Posts

Posted (edited)

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

Share this post


Link to post
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 |
+---------------------+---------------------+----------+

 

Share this post


Link to post
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');

 

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

TIMEDIFF returns a time string, not a number. If you need a number then try TIMESTAMPDIFF.

Share this post


Link to post
Share on other sites
7 minutes ago, requinix said:

TIMEDIFF returns a time string, not a number. If you need a number then try TIMESTAMPDIFF.

Thanks for the advise but other way that worked for me is change the datatype of the "duration" field in db to time. Attached is the screenshot

correct.png

Share this post


Link to post
Share on other sites

You know what would be even better than that? Not storing the duration. Like Barand said, you don't have to bother storing it and then worrying about what to do when values change when you can just call TIMEDIFF whenever you need it.

Share this post


Link to post
Share on other sites

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.