php - 如何从数据库的时间戳字段过滤php中的日期月份和年份

标签 php mysql database

我在 php 中有一个 cms,我在数据库中制作预订记录,我想制作一份有关特定月份/年份的预订的报告,以便当我放置时我需要根据需要过滤这些记录月份的所有记录都显示在我的当月表格中,这也可以应用于年份记录...

<script>
    function myFunction() {
        console.log("hello");
        var input, filter, table, tr, td, i, txtValue;
        input = document.getElementById("myinput");
        filter = input.options[input.selectedIndex].text;
        date = new Date(filter);
        month = date.getMonth().toString();
        filter = month;
        //console.log(filter);
        table = document.getElementById("mytable");
        tr = table.getElementsByTagName("tr");
        for (i = 0; i < tr.length; i++) {
            td = tr[i].getElementsByTagName("td")[6];
            if (td) {
                // txtValue = td.textContent || td.innerText;
                txtValue = new Date(td.innerText).getMonth().toString();
                console.log(txtValue);
                if (txtValue.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
</script>
Filter Date:
<div class="form-group">
    <label for="">Month</label>
    <select name="date" id="myinput">
        <?php
        $query = "SELECT * FROM package_booking WHERE date";
        $select_date = mysqli_query($connection, $query);
        confirmQuery($select_date);
        while ($row = mysqli_fetch_assoc($select_date)) {
            $pb_id = $row['pb_id'];
            $date = $row['date'];
            $mon = date('m', strtotime($date));
            echo "<option value=''>{$date}</option>";
        }
        ?>
    </select>
</div>

我编写的这段代码是用 JS 编写的,但现在我需要知道可以过滤数据库中记录的 php 部分,以便获取一个月的记录。

最佳答案

您的代码格式不正确,但您可能想尝试以下操作:

SELECT * FROM `your_db_name` WHERE Year(the_date_field_in_your_db) = 'the_year_you_need' AND MONTH(the_date_field_in_your_db)= 'date_numerical_value';

日期通常以数字形式存储(1 表示一月,2 表示二月,等等)。

关于php - 如何从数据库的时间戳字段过滤php中的日期月份和年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221320/

相关文章:

MySQL创建表并从另一个表设置属性?

mysql - 具有唯一键约束的数据库中的重复行

java - MySQL 未连接

mysql - 三表MySQL LEFT JOIN不返回不涉及一张表的行

php - 多插入一种形式

mysql - SQL 一对多关系,但是多对一表关系

javascript - 通过 AJAX 插入创建时 HTML 代码不起作用

php - 如何获取 curl exec 响应

javascript - 循环插件幻灯片未执行,但图像显示在彼此上方

php - 数据未使用php添加到数据库中