我有一个包含事件的表格,并希望根据用户选择的日期范围列出所有事件。我使用 jQuery datepicker,从中获取值并放入 sql 查询以显示所选日期范围内的事件。我的日期格式是 YYYY-mm-dd (fe 2016-05-05),但是当我从日期选择器中选择日期时,它是 (YYYY-05-05)。由于某种原因,它不会将 yyyy 转换为日期,而是将月份和日期转换为日期。这是我的代码:
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var firstdate = $('#firstdatepicker').datepicker({ dateFormat: 'YYYY- mm-dd' }).val();
});
</script>
<script>
$(function() {
var lastdate = $('#lastdatepicker').datepicker({ dateFormat: 'YYYY-mm- dd' }).val();
});
</script>
</head>
<body>
<form method="post" action="archive.php">
<p>First Date: <input name = "firstdatepicker" type="text" id="firstdatepicker"></p>
<p>Last Date: <input name = "lastdatepicker" type="text" id="lastdatepicker"></p>
<input type="submit" value="Get Data" name="data"/>
</form>
我在 php 中的查询
if(!isset($_POST['filters']) && isset($_POST['firstdatepicker']) && isset($_POST['lastdatepicker']) ){
$firstdate = $_POST['firstdatepicker'];
$lastdate = $_POST['lastdatepicker'];
$query= "SELECT *
FROM Event
WHERE Event.`start_date` BETWEEN '$firstdate' and '$lastdate'
ORDER BY start_date DESC";
}