douweng7308 2013-04-16 01:19
浏览 15
已采纳

如何在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

  • 写回答

2条回答 默认 最新

  • duanlieshuang5330 2013-04-16 01:32
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页