1

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 (f.e 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";

}

1
  • Please see the DOC regarding valid date format Commented Dec 11, 2016 at 15:55

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thanks a lot! It helped
0

Use yy as 4 digit year placeholder: dateFormat: "yy-mm-dd"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.