1

I've using the DATEDIFF method to get the difference b/w two dates obtained using the date(YYYY:mm:DD HH:MM:ss) function but getting the output 0 every time, even when the difference is more than 1 day.

here's my code -

if(isset($_GET['rt']))
{
$dateis=$_GET['rt'];
}
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s');
if(mysqli_query($conn, "UPDATE issued_books SET Date_returned = '$date' WHERE Book_id = '$bk_id' AND Date_issued = '$dateis'"))
{
    $fine = sprintf("SELECT DATEDIFF('%s','%s') AS DAYS",
               '$date', '$dateis');
    $fine = $fine*10;
    mysqli_query($conn, "UPDATE member_db SET Fine_amt = (Fine_amt + '$fine') WHERE Member_id = '$mem'");
}

any kinda help would be really appreciated, thanks :)

2 Answers2

0

1.remove single quotes for '$date' and '$dateis'

$fine = sprintf("SELECT DATEDIFF('%s','%s') AS DAYS", $date, $dateis);

2.$fine = $fine*10; // this will not work since query is not yet executed in mysql, that's why you will always get undesired result

And always sanitize your parameters to avoid sql injection

cletsimon
  • 61
  • 5
0

Nevermind, I've found the answer here - https://stackoverflow.com/a/676828/7744098 using the strtotime() & floor methods.

Thank you all for commenting.

Community
  • 1
  • 1