我正在尝试按时间对此查询进行排序。
我有一个游戏匹配系统。我想从当地时区获得接下来5场比赛的名单。
<?php
include_once "include/dbcompo.php";
$q=mysqli_query($con, "SELECT * FROM kamper ORDER BY tid LIMIT 5");
while($row = mysqli_fetch_array($q))
{
$clan1 = $row['clan1'];
$clan2 = $row['clan2'];
$server = $row['server'];
$tid = $row['tid'];
echo $clan1." ".$clan2." ".$server." ".$tid;
echo "<br />";
}
?>
答案 0 :(得分:0)
在您的查询中添加WHERE
子句:WHERE tid > NOW()
。
使用NOW()
您需要花费服务器的时间,也许您应该将其替换为new DateTime(null)->getTimestamp()
类似的东西:
<?php
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > NOW() ORDER BY tid LIMIT 5');
// or
mysqli_query($con, 'SELECT * FROM kamper WHERE tid > '.new DateTime(null)->getTimestamp().' ORDER BY tid LIMIT 5');
?>
答案 1 :(得分:0)
两个选项:
第一个选项:将时间字段设为数字字段并在PHP中排序:
$queryResult = mysqli_query($con, $query);
while($row = mysqli_fetch_array($queryResult) {
$oldArray[$row['time'] = $row;
}
$array = ksort($oldArray);
foreach($array as $time=>$row) {
// do something
}
第二个选项:制作子查询
SELECT * FROM (
SELECT * FROM kamper WHERE timezone = 'UTC' ORDER BY tid
) LIMIT 5