比较多维数组中的两个DATETIME字段并返回值或索引

时间:2015-06-12 20:37:12

标签: php mysql arrays multidimensional-array comparison

我有一个用户表。我正在查看每行2 DATETIME。我会查看$lastLog日期和$lastReceived(针对最后收到的潜在客户),并确定哪个更新,并成为他们的$eligibleTime。然后,我们将查找最早的$eligibleTime,以选择哪个用户应该获得潜在客户。我正在过滤用户,以确保他们获得了潜在客户所处的适当状态的许可,并且他们还没有达到他们的每日配额。

我想也许我构建了多d数组错误,应该将所有$lastLog日期和$lastReceived日期分组到他们自己的数组中并运行max($array)或者其他东西像这样。

我怎么看这个?我用:

构建了一个多维数组
    $sql = "SELECT `lastLog`,`firstName`,`lastReceived` FROM customers";
    $sql = mysqli_query($conn,$sql);
    $result = array();
    while($line = mysqli_fetch_assoc($sql)){
      $result[] = $line;
    }

它返回以下数据:

> Array ( [0] => Array ( [lastLog] => 2015-06-12 02:00:00 [firstName] => Nathaniel [lastReceived] => 2015-06-12 05:16:10 ) [1] => Array ( [lastLog] => 2015-06-12 01:00:00 [firstName] => Ignacio [lastReceived] => 2015-06-01 10:00:00 ) [2] => Array ( [lastLog] => 2015-06-12 00:00:00 [firstName] => James [lastReceived] => 2015-06-08 00:00:00 ) [3] => Array ( [lastLog] => 2015-06-12 04:00:00 [firstName] => Robert [lastReceived] => 2015-06-10 00:00:00 ) ) 

谢谢你,nate

1 个答案:

答案 0 :(得分:0)

感谢Marc B,这已经解决了。

$sql = mysqli_query($conn,
   "SELECT firstName, greatest(lastLog, lastReceived) AS eligibleTime 
    FROM customers 
    ORDER BY eligibleTime DESC
    LIMIT 1");
$result = mysqli_fetch_assoc($sql);
print_r($result);