Jump to content

Subtract times in database from current time.


mikeinaustin

Recommended Posts

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 by Ch0cu3r
Added code tags
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.