Compare two DATETIME fields from multidimensional array and return a value or index

95 Views Asked by At

I have a user table. I have 2 DATETIME per row that I'm looking at. I will look at $lastLog date and $lastReceived (for last received lead) and determine which is newer and that becomes their $eligibleTime. We will then look for the oldest $eligibleTime to select which user should receive the lead. I am filtering through users to make sure they are licensed in the appropriate state that the lead is in and also that they haven't reached their daily quota.

I'm thinking maybe I built the multi-d array wrong and should have grouped instead all of the $lastLog dates and $lastReceived dates into their own arrays and run max($array) or something like this.

How can I look at this? I've built a multidimensional array using:

    $sql = "SELECT `lastLog`,`firstName`,`lastReceived` FROM customers";
    $sql = mysqli_query($conn,$sql);
    $result = array();
    while($line = mysqli_fetch_assoc($sql)){
      $result[] = $line;
    }

It is returning the following data:

> 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 ) ) 

Thank you, nate

1

There are 1 best solutions below

0
On BEST ANSWER

Thanks to Marc B this got solved.

$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);