如何在PHP中按最高日期列出

Hey guys so I am having some trouble using the following:

for ($i = 0; $i < count($noncompUsername); $i++)
{
    $tsql ="SELECT firstname,lastname,email,phone,statuschangedate FROM csvdata WHERE username = :username ORDER BY statuschangedate";
    $tgetmeminfo=$DBH->prepare($tsql);
    $tgetmeminfo->execute(array(':username' => $noncompUsername[$i]));
    while ($trow = $tgetmeminfo->fetch(PDO::FETCH_ASSOC)){

        $csvFirst = $trow['firstname'];
        $csvLast = $trow['lastname'];
        $csvEmail = $trow['email'];
        $csvPhone = $trow['phone'];
        $csvDate = $trow['statuschangedate'];
        $timediff = strtotime($date) - strtotime($csvDate);
        $timediff = floor($timediff/86400);

        $sql ="SELECT MailingAdrs FROM insuranceverificationdisclaimer WHERE TraineeUsername = :tusername";
        $getmeminfo=$DBH->prepare($sql);
        $getmeminfo->execute(array(':tusername' => $noncompUsername[$i]));
        while ($row = $getmeminfo->fetch(PDO::FETCH_ASSOC)){

            $csvAddrs = $row['MailingAdrs'];
            $change = 1;
        }
        if($change != 1)
        {
            $csvAddrs = "No address";
        }
        $change = 0;
        echo "$timediff, $csvFirst $csvLast, $csvEmail, $csvPhone, $csvAddrs";
    }
    echo "
    <br>
    ";
}

Now this works but the part I want to point out is the $tsql ="SELECT firstname,lastname,email,phone,statuschangedate FROM csvdata WHERE username = :username ORDER BY statuschangedate"; - now when I do this and get the integer of the statuschangedate to the current date and print it out as an integer, it is not ordered properly based on the date.

So I need to get this to order by the oldest date on top and as follows...

Thank you!

David

If you want to order based on $timediff you should change the ORDER clause to this:

ORDER BY DATEDIFF(:date, statuschangedate)

Granted, this should actually give the same ordering you already had, but you could at least use this expression to save some processing in PHP itself :)

Order the date in descending order (using DESC):

... ORDER BY statuschangedate DESC