0

There are few dates in the database. I must select another date using a jQuery datepicker. But the restriction is that, I must select a date(in the datepicker) which is only in the database and display this validated date in an 'alert'. The problem here is that in mysql, the date format is yyyy-mm-dd and in datepicker it is mm-dd-yyyy. How do I validate these two dates? I have searched a lot for a solution, but didn't find any.

The database date values and the datepicker selection are in the same file. associate.php

echo "<center><table border=2 cellpadding=25 cellspacing=0 width=200 style='border-collapse:collapse;table-layout:fixed;width:210pt'></center>
<tr>
<th> S.No</th>
<th>Date</th>
<th>Event</th>
</tr>";
while($record = mysql_fetch_array($myData)){
  echo "<tr>";
  echo "<td>" . $record['S.No'] . "</td>";
  echo "<td>" . $record['Date'] . "</td>"; 
  echo "<td>" . $record['Event'] . "</td>";
  echo "</tr>";
}
echo "</table>";

echo "<form id=\"dateform\" name=\"dateform\" action=\"#\" method=\"POST\"><br><br>
<table>
<tr><td>
<b>Select a date &nbsp;&nbsp;<b></td><td><input id=\"datepicker\" name=\"date\"size=\"15\"/></td></tr>

</table>
<br>
<input type=\"submit\" name=\"submit\" value=\"Submit\" />&nbsp;
</form>";

The following is the code to select the date from datepicker.

<script>
$(document).ready(function () {
  $("#datepicker").datepicker();
  $('form#dateform').submit(function(){ 
    var aselectedDate = $('#datepicker').val();
    if(aselectedDate !=''){
      alert('You have selected ' + aselectedDate);
    }
    return false;
  });
});
</script>
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
newbie
  • 23
  • 5

2 Answers2

1

The simplest solution is to configure Datepicker to produce the same date format as you receive from your database, i.e.:

<script>
$(document).ready(function () {
    $("#datepicker").datepicker({
       dateFormat: "yy-mm-dd"   
    });

// other code ...

</script>

Check out jQuery Datepicker reference for more details how to deal with different date formats:

http://api.jqueryui.com/datepicker/

P.S. In particular, check out a date formatting syntax:

http://api.jqueryui.com/datepicker/#utility-formatDate

For example, it does not support yyyy. If you need four-digit year, use yy.

Andrew Simontsev
  • 1,078
  • 9
  • 18
  • Cool, it worked... How do I change the format of dates that are retrieved from the database? I have googled and found that there isn't a way to format the dates within the database. So, in my code..these dates are retrieved in yyyy-mm-dd format.. how can I display them as dd-mm-yyyy? – newbie Aug 15 '15 at 12:09
  • I am not a big guru in PHP, but I think the format method of DateTime should help: http://php.net/manual/en/datetime.format.php – Andrew Simontsev Aug 20 '15 at 06:15
1

Try this:

alert('You have selected ' + <? echo date("mm-dd-yyyy", strtotime(selectedDate))); ?>
Funny Frontend
  • 3,807
  • 11
  • 37
  • 56