PHP回应2015-06-00为第31个'经过查询

时间:2015-04-11 14:25:39

标签: php

在我的数据库中,我有一行名为game_release,它是一个日期字段。

没有特定发布日期的游戏(例如我们知道它将在6月发布)将以00输入,例如2015-06-00。这是有效的,如果我的查询包含00,一切都很好,但是......

而不是将Day显示为0,它被回显为31.我不知道为什么,显然PHP在日期不完整时遇到了麻烦。

这是我的代码:

                    <?php

                    $platform_id = 7;
                    $query="SELECT * FROM game WHERE game_id IN (SELECT game_id FROM game_platform WHERE platform_id=".$platform_id.") AND game_release BETWEEN '2015-12-01' AND '2015-12-31' ORDER BY game_release";

                    $result=mysqli_query($link,$query) or die (mysqli_error());
                    if (mysqli_num_rows($result) > 0)
                    {

                            echo"<table border=1>
                            <tr>
                                <th width=40px>Day</th>
                                <th width=270px>Title</th>
                                <th width=203px>Genre</th>
                                <th width=203px>Developer</th>
                                <th width=205px>Publisher</th>
                                <th width=61px>Retail?</th>
                                <th width=30px height=30px></th>
                                <th width=104px>Note</th>
                            </tr>";

                            while($row=mysqli_fetch_assoc($result))
                           {
                            echo"
                            <tr>
                                <td> ". (new DateTime($row['game_release']))->format("j") ." </td>
                                <td>{$row['game_name']}</td>

                                <td>";
                                $query="SELECT * FROM genre WHERE genre_id=".$row['game_genre']; 
                                $genreresult=mysqli_query($link,$query);                                    $genrerow=mysqli_fetch_assoc($genreresult);
                            echo $genrerow['genre_name'];
                            echo "</td>
                                <td>{$row['game_dev']}</td>
                                <td>{$row['game_pub']}</td>
                                <td>{$row['game_type']}</td>
                                <td><a href=\"{$row['game_site']}\" target=\"_blank\"><img src=\"images/officialwebsite.png\" title=\"Official website\"/></a>
                                    <a href=\"{$row['game_trailer']}\" target=\"_blank\"><img src=\"images/youtube.png\" title=\"Trailer\"/></a></td>
                                <td>{$row['game_note']}</td>
                            </tr>";


                           }

                           echo"</table>";
                           }

                            else
                            {
                            echo "There don't seem to be any confirmed nor rumoured releases this month!";
                            }
                    ?>

此代码生成日期:

<td> ". (new DateTime($row['game_release']))->format("j") ." </td>

我可以做些什么让00回显00,或者你们知道另一种方式来清楚地表明日期是不完整的吗?我仍然希望它能够在显示月份发布的表格中得到回应。

1 个答案:

答案 0 :(得分:1)

问题是该日期在技术上是无效日期。因此,如果日期有效,您可以使用checkdate()进行简单检查,如果不是这样显示00

list($year, $month, $day) = explode("-", $row['game_release']);
if (checkdate($month, $day, $year)) {
    echo "<td> ". (new DateTime($row['game_release']))->format("j") ." </td>";
} else {
    echo "<td>00</td>";
}

所以你的代码应该是这样的:

while($row=mysqli_fetch_assoc($result)) {
    list($year, $month, $day) = explode("-", $row['game_release']);
    if (checkdate($month, $day, $year)) {
        $date = (new DateTime($row['game_release']))->format("j");
    } else {
        $date = "00";
    }

    echo"<tr>
            <td> ". $date ." </td>
            <td>{$row['game_name']}</td>
        <td>";
    //...