简体   繁体   中英

How to calculate difference MySQL date in PHP?

I try lots of things but I can not find the solution.

$con=mysqli_connect('localhost','root','root','hidroist');
if(! $con)
    echo "Error" . mysqli_error($con);
$maxresult = mysqli_query($con, "SELECT MAX(var) as 'max' FROM date");
$row = mysqli_fetch_array($maxresult);
echo $row['max'];

 $minresult = mysqli_query($baglanti, "SELECT min(var) as 'min' FROM date;");
 $row = mysqli_fetch_array($minresult);
 echo "<br>";
 echo $row['min'];
 echo "<br>";

I need to calculate difference between $row['min'] and $row['max'] after that database should delete all data from table .

you coudl do this with a single query and use backtics for reserver or keyword or column name alis and not single quote

$allresult = mysqli_query($con, "SELECT MAX(var) as `max` ,
         min(var) as `min` , MAX(var) - min(var) as my_diff FROM date");

for my this you can access to the value using the same alias name eg as in you code using $row

     echo $row['my_diff'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM