以分钟为单位计算PHP中的DateDiff

时间:2013-07-13 03:00:18

标签: php mysql

<?php

$sql_apr = " SELECT SUM ( meter * minute ) FROM table";
$rs_apr = @mysql_query($sql_apr);
$total_apr = @mysql_fetch_array($rs_apr);
$try4 = $total_apr['SUM(meter * minute'];

while ($rs_t = @mysql_fetch_array($rs_t)) {
    $minute = '';

    $sql_t = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
    $rs_t = @mysql_query($sql_t);
    $minute = $rs_t['minute'];
}

&GT;

2 个答案:

答案 0 :(得分:1)

您正在查看错误的结果:

$rs_t=@mysql_query($sql_t);
$minute = $total_t['minute'];

应该是

$rs_t=@mysql_query($sql_t);
$minute = $rs_t['minute'];
//         ^ use the rs_t result, not the result from the first query

您也是循环的变量的结果。我非常怀疑这个while循环将永远结束。您正在循环$rs_t的结果,然后在循环内重新分配$rs_t

答案 1 :(得分:0)

为什么不以正确的顺序执行此操作

$query = "SELECT DATEDIFF(MINUTE,'e_date e_time','s_date s_time') AS minute";
$results = @mysql_query($query);
$row = @mysql_fetch_array($results);
$minute = $row['minute'];

print_r($minute);

$sql_apr = " SELECT SUM ( meter * " . $minute . " ) AS my_sum FROM table";
$rs_apr = @mysql_query($sql_apr);
$total_apr = @mysql_fetch_array($rs_apr);
$try4 = $total_apr['my_sum']; 

在运行查询之前,您尝试获取结果(在while中)。

修改

$query = "SELECT TIMESTAMPDIFF(MINUTE,'s_date s_time','e_date e_time') AS minute";