简体   繁体   English

如何在 PHP 中计算差异 MySQL 日期?

[英]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 .我需要计算 $row['min'] 和 $row['max'] 在该数据库应该删除 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对于我的这个,您可以使用相同的别名访问该值,例如在您使用 $row 的代码中

     echo $row['my_diff'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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