我有一个用户表。我正在查看的每行有 2 个 DATETIME
。我将查看 $lastLog
日期和 $lastReceived
(最后收到的潜在客户),并确定哪个较新,并成为他们的 $eligibleTime
。然后,我们将查找最早的 $eligibleTime
来选择哪个用户应接收潜在客户。我正在筛选用户,以确保他们在潜在客户所在的适当状态下获得许可,并且他们尚未达到每日配额。
我想也许我构建的多维数组是错误的,应该将所有 $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 ) )
谢谢你,内特
最佳答案
感谢 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);
关于php - 比较多维数组中的两个 DATETIME 字段并返回值或索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30811801/