0

I have a database that stores the time when a form is submitted. There are two forms thus 2 different timings are stored. How do I automatically get the bigger time to minus the smaller time and display the results.

Things I've tried: (a part of index.php)

$timeR = 'timeR';
$timeB = 'timeB';
if ($timeR > $timeB) {
    echo ( strtotime($timeR) - strtotime($timeB) );
}
else{
    echo ( strtotime($timeB) - strtotime($timeR) );
}

timeR and timeB is the row name that is used to store the time. The code above doesn't work, no errors but nothing is printing.

$row1 = mysqli_fetch_array($result1)
$row = mysqli_fetch_array($result);
echo ( strtotime($row1['timeB']) - strtotime($row['timeR']) );

This code above works and prints something but the numbers printed are wrong??

Time is stored using another page that inserts data into the database which then redirects you to index.php

Time is stored like this on insertblue.php:

$currenttimeB = date('Y-m-d - H:i:s');
echo $sql="UPDATE indextableblueupdated SET FTPBreach = '$_POST[FTPBreach]', 
                                        VictimIP = '$_POST[VictimIP]', 
                                        VictimPass = '$_POST[VictimPass]', 
                                        SQLUser = '$_POST[SQLUser]', 
                                        SQLPass = '$_POST[SQLPass]', 
                                        message = '$_POST[message]', 
                                        timeB = '$currenttimeB' WHERE user = '2'" ;

Similarly I have another page called insertred.php which does the exact same thing as insertblue.php

Coln
  • 29
  • 6
  • I've read that post and honestly i have no idea what's going on. Am pretty new to php / hmtl. @faintsignal Also i do not have issue with posting to database but rather am trying to find out how can i retrieve and do calculations between 2 timings from a database. – Coln Dec 24 '18 at 04:35
  • Tried something else and edited post to match current situation – Coln Dec 24 '18 at 04:39
  • 1
    Never directly use `$_POST` or `$_GET` variables in SQL statement, it might lead to SQL Injection attack. – Raptor Dec 24 '18 at 04:45
  • Check this post https://stackoverflow.com/questions/365191/how-to-get-time-difference-in-minutes-in-php – Satya Dec 24 '18 at 04:48
  • @Raptor I'm running this on a local network designed to allow "students" to learn how sql injection work. I'm also a student just that i'm a third year and i'm suppose to design a "game" for my juniors. Its a project. – Coln Dec 24 '18 at 04:51
  • 1
    There are several improvements you can perform: *1.* You can do the time difference calculations in the MySQL server using `DATEDIFF()` and then `UPDATE` using the same query. In this way, you don't need to maintain multiple scripts; *2.* For the date format, I hope you didn't save the dates using `VARCHAR` data type, you should save it as `DATETIME`; *3.* avoid using `strtotime()` without checking the result (whether it's a valid date or not). – Raptor Dec 24 '18 at 06:58
  • @Raptor hi, i saved the time as TIME, should i replace strtotime() with something else? – Coln Dec 26 '18 at 05:30
  • 1
    You cannot fit the value of `date('Y-m-d - H:i:s')` into `TIME` column. Change the data type and re-insert all the data – Raptor Dec 27 '18 at 07:46

1 Answers1

2

Try to convert timeR and timeB to time before comparing:

$timeR = strtotime($row1['timeR']); //assuming this from your table
$timeB = strtotime($row1['timeB']); //assuming this from your table
if ($timeR > $timeB) {
    echo ( $timeR - timeB );
}
else{
    echo ( $timeB - $timeR );
}
McBern
  • 549
  • 1
  • 4
  • 8