1

I want is to calculate the time difference from to times intime and outtime. I did the below code and at the end face an error

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp\www\testsite\time_test.php on line 105

<?php
$con = mysqli_connect("localhost","root","","testsite") OR die("Could not connect: ".mysqli_error());
    
if (isset($_POST['isubmit'])) {
    // Check the cookie exits or not
    if (isset($_COOKIE['uid'])) {
        $_COOKIE['uid'];
    }

    $uid = $_COOKIE['uid'];

    echo "Hello user: ".$uid;

    $isql = mysqli_query($con, "INSERT INTO `time`(`uid`, `intime`, `date`) VALUES ('$uid',CURTIME(),CURDATE())");
    $lid = mysqli_insert_id($con);

    echo $lid;
}
if (isset($_POST['osubmit'])) {
    if(isset($_COOKIE['uid'])) {
        $_COOKIE['uid'];
    }

    $uid = $_COOKIE['uid'];
    $ssql= mysqli_query($con,"SELECT `intime`, `outtime`, `date` FROM `time` WHERE `uid`=".$uid);
    
    while ($row = mysqli_fetch_array($ssql)) {
        $intime = $row['intime'];
        $outtime= $row['outtime'];  
        $date   = $row['date'];
        
        if(!empty($date)){
            $tmp = mysqli_query($con, "SELECT CURTIME()");
            $lid = mysqli_insert_id($con);
                    
            $temp= strtotime($outtime) - strtotime($intime);
                    
            echo $temp;
            //below line is line number 105
            $usql= "UPDATE `time` SET `outtime`= $tmp,`diff`= '$temp' WHERE `id`=".$lid." AND `uid`=".$uid;
            $run = mysqli_query($con, $usql);
            if (!$run) {
                die("Update database query error: ". mysqli_error());
            }
        }
    }
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Manya Singh
  • 131
  • 1
  • 1
  • 10
  • `$tmp = mysqli_query($con, "SELECT CURTIME()");` might be a bit over kill don't you think ? Unless your mysql server is different from your apache server and runs under another GMT – Brewal Dec 03 '14 at 09:13
  • I am always doing first on local drive than after upload it and change the necessary things which i point out in diary. Through this I get the time from my computer and it's good at this time. I just want to get rid from the error... – Manya Singh Dec 03 '14 at 09:16

1 Answers1

0

Your problem is that your $tmp var is a query, not a result of the query. You have to fetch the result first. That is why you get this error. But you could get the time with PHP, without having to do a query :

$tmp = date('H:i:s');

Also, your $temp variable will result as an integer. Use the date function to format it :

$temp = date('H:i:s', strtotime($outtime) - strtotime($intime));

Also be aware that you are subject to SQL injections : your $uid is a cookie that is not safe. You might want to cast it as an integer :

$uid = (int)$_COOKIE['uid'];

Or you can do it better and use prepared statements

Brewal
  • 8,067
  • 2
  • 24
  • 37
  • I replace the $tmp to $otime in the arithmatic operation but when I did this I face an same error again with one warning – Manya Singh Dec 03 '14 at 09:22
  • Dump your variables and see what you got before making your `$usql` – Brewal Dec 03 '14 at 09:23
  • okay I used int and after that as I am taking the curtime() for both time intime and outtime than in $temp i get this type of value 1417643818. however i convert it in to h.m.s but can't see any change – Manya Singh Dec 03 '14 at 09:34
  • Yeah not when I enter the intime 15.10.00 and outtime is 15.11.00 than I see the difference is 09:49:48 rather than 00.01.00 anyway thanks and what you think is there any problem in my update query? – Manya Singh Dec 03 '14 at 09:48
  • I don't understand... Do you still have a problem ? It works for me : http://codepad.org/1j0mKzq3 `var_dump` your variables and your query. – Brewal Dec 03 '14 at 09:56