我在 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/