mikeinaustin Posted October 9, 2014 Share Posted October 9, 2014 (edited) Hello, I am trying to subtract the current time from a list of times in a mysql database. The first page inserts the time into mysql. <?php $var_Time = date("h:i:s"); $mysqli = new mysqli('localhost','root','blah','test'); if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); } $insert_row = $mysqli->query("REPLACE INTO test_table (id, time) Values(NULL, '$var_Time')"); if($insert_row){ while (false); }else{ die('Error : ('. $mysqli->errno .') '. $mysqli->error); } $mysqli->close(); ?> The second page is supposed to iterate through all of the times in the database and subtract the current time, but i cant figure out how to do it. <?php $mysqli = new mysqli('localhost','root','blah','test'); if ($mysqli->connect_error) { die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error); } $query = "SELECT * FROM `test_table`"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); $var_set_time = stripslashes($row['time']); $var_current_time = date("h:i:s"); $diff = strtotime( $var_current_time .' UTC' ) - strtotime( $var_set_time .' UTC'); echo $diff; ?> Edited October 9, 2014 by Ch0cu3r Added code tags Quote Link to comment https://forums.phpfreaks.com/topic/291539-subtract-times-in-database-from-current-time/ Share on other sites More sharing options...
Ch0cu3r Posted October 9, 2014 Share Posted October 9, 2014 What you trying to do? Calculate the difference between two dates? Have a look at dateTime::diff NOTE: Please wrap code within tags when posting code. For now I have edited your post for you. Quote Link to comment https://forums.phpfreaks.com/topic/291539-subtract-times-in-database-from-current-time/#findComment-1493177 Share on other sites More sharing options...
Barand Posted October 9, 2014 Share Posted October 9, 2014 Or SELECT id , TIMEDIFF(CURTIME(), time) as diff FROM test_table 1 Quote Link to comment https://forums.phpfreaks.com/topic/291539-subtract-times-in-database-from-current-time/#findComment-1493179 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.