需要帮助将我的datediff查询放在此代码中

时间:2018-06-25 06:44:55

标签: php mysql

我可以在此代码中插入datediff查询

$search = $_POST['search'];
$result = mysql_query("SELECT * FROM documents where docnum  "My Search Bar"='$_SESSION[test2]' and doctitle LIKE '%$search%' order by docprim desc");
while($row = mysql_fetch_array($result))
{
   echo "<tr class ='hovered'>";

   echo "<td width = '100px' class='text-left'>";  
   echo $row['docnum'];
   echo "</td>";

   echo "<td width = '100px' class='text-left'>";
   echo $row['datein']." to ".$row['dateout'];
   echo "</td>";

}

这是我的代码的示例。我想从表的datein和dateout字段中添加持续时间。

2 个答案:

答案 0 :(得分:0)

您需要修改您的SQL查询字符串。

$startdate = date('Y-m-d');
$enddate = date('Y-m-d', strtotime("-3 days"));
$result = mysql_query("SELECT * FROM documents where docnum ='".$_SESSION['test2']."' and doctitle LIKE '%$search%' and DATEDIFF('".$startdate."', '".$enddate."') > 0 order by docprim desc");

您的SQL查询字符串也有问题。让我知道您是否也需要帮助。

希望这会有所帮助。

答案 1 :(得分:0)

在sql-query中的'*'之前插入TIMESTAMPDIFF:

SELECT TIMESTAMPDIFF(SECOND, d.datein, d.dateout) AS timediff, m.* FROM documents m;

并用作:

echo $row['datein']." to ".$row['dateout'] . " (" . $row['timediff'] . ")" ;

$ row ['timediff']将显示秒。