ramiwahdan Posted March 13, 2020 Share Posted March 13, 2020 (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 March 13, 2020 by ramiwahdan thoughts Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/ Share on other sites More sharing options...
Barand Posted March 13, 2020 Share Posted March 13, 2020 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 | +---------------------+---------------------+----------+ Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575404 Share on other sites More sharing options...
ramiwahdan Posted March 13, 2020 Author Share Posted March 13, 2020 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'); Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575405 Share on other sites More sharing options...
Barand Posted March 13, 2020 Share Posted March 13, 2020 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 2 Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575406 Share on other sites More sharing options...
ramiwahdan Posted March 13, 2020 Author Share Posted March 13, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575407 Share on other sites More sharing options...
requinix Posted March 13, 2020 Share Posted March 13, 2020 TIMEDIFF returns a time string, not a number. If you need a number then try TIMESTAMPDIFF. Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575408 Share on other sites More sharing options...
ramiwahdan Posted March 13, 2020 Author Share Posted March 13, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575410 Share on other sites More sharing options...
requinix Posted March 13, 2020 Share Posted March 13, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310283-timestamps-differences/#findComment-1575411 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.