i have a table with events and want to list all events based on the date range the user will pick. I use jQuery datepicker, take value from it and put to sql query to show the events be†ween selected date range. My date format is YYYY-mm-dd (fe 2016-05-05),but when I select the date from date picker it comes as (YYYY-05-05). For some reason it does not convert yyyy to date, but does for month and day. here is my code:
<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>
And my query in 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";
}
YYYY
is not a valid date format, you should use yy
(two lower case y)
also, just a suggestion - you should escape user input ( post
) with some kind of function like mysqli_real_escape_string
or whatever fits your SQL connection
Change to:
var lastdate = $('#lastdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
and
var firstdate = $('#firstdatepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val();
Have a look at: http://api.jqueryui.com/datepicker/#option-dateFormat
使用yy作为4位数字的年份占位符: dateFormat: "yy-mm-dd"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.