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

时间:2019-07-26 13:42:20

标签: 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部分,以便获得一个月的记录。

1 个答案:

答案 0 :(得分:0)

您的代码格式不正确,但是您可能想要尝试类似的操作:

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表示1月,2表示2月,依此类推)。