Hello i have the following code that will display dates based on dropdown of month and year
<html>
<head>
<script>
function showDate() {
var str=document.getElementById("mymonth").value;
var str1=document.getElementById("myyear").value;
if (str==0) {// if nothing is selected from first drop down
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getdate.php?selectedmonth="+str+"&selectedyear="+str1,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="mymonth" id="mymonth" onchange="showDate()">
<option>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">Mac</option>
<option value="04">April</option>
<option value="05">Mei</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="myyear" id="myyear" onchange="showDate()">
<option>Select Year:</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>
</form>
<br>
<div id="txtHint"><b>Date group by month/year</b></div>
</body>
</html>
<?php
$selectedmonth = intval($_GET['selectedmonth']);
$selectedyear = intval($_GET['selectedyear']);
$month = "$selectedmonth";
$year = "$selectedyear";
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime("+1 month", $start_time);
echo "<table border='1'>";
for($i=$start_time; $i<$end_time; $i+=86400)
{
$list = date('d M Y (D)', $i);
echo "<tr><td>";
echo $list;
echo "</td>";
echo "<td> </td>";
echo "</tr>";
}
echo "</table>";
?>
Right now when we select the month and year for first time it is showing incorrect result.
As example when we pick June it will display
31 May 2001 until 30 Jun 2001
and if we select 2016
, it will display 2015
dates.
After that when we select again, the month or year it will show correct result.:stars:
Add value attribute to the default select option.
<option value="">Select Month</option>
<option value="">Select Year:</option>
Put the following condition before making Ajax request.
if(str == '' || str1 == ''){
return;
}
//Your Ajax goes here...
Add the timezone to the PHP file, in the beginign of the file, below <?php
open tag.
date_default_timezone_set("Asia/Kolkata"); //Change your timezone accordingly. Here it's set to Kolkatta, India.
I hope it helps you !
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.