I have a calendar using PHP and I have a problem going to the next month like for example today's month is JANUARY when I go to the next month instead of showing FEBRUARY it's showing the month of MARCH, I didn't have this problem until January 29, I did some test to find the solution to my problem by changing the date, when I change my date to Jan 28, my code works perfectly fine, I think the problem is that my calendar is searching for a month, day, year and because some months don't have 31 as their day and February doesn't have 29,30,31 as its day it's overlooking some months.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
function goLastMonth(month, year){
if(month == 1){
--year;
month = 13;
}
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+(month-1)+"&year="+year;
}
function goNextMonth(month, year){
if(month == 12){
++year
month = 0;
}
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+(month+1)+"&year="+year;
}
</script>
</head>
<body>
<?php
if(isset($_GET['day'])){
$day = $_GET['day'];
}else{
$day = date("j");
}
if(isset($_GET['month'])){
$month = $_GET['month'];
}else{
$month = date("n");
}
if(isset($_GET['year'])){
$year = $_GET['year'];
}else{
$year = date("Y");
}
date_default_timezone_set('Asia/Manila');
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<table border="1">
<tr>
<td><input style='width:50px;' type='button' value='<' name='previousbutton' onclick="goLastMonth(<?php echo $month.",".$year?>)">
</td>
<td colspan="5"><?php echo $monthName. "," .$year;?></td>
<td><input style='width:50px;' type='button' value='>' name='nextbutton' onclick="goNextMonth(<?php echo $month.",".$year?>)"></td>
</tr>
<tr>
<td style="width:50px;">Sun</td>
<td style="width:50px;">Mon</td>
<td style="width:50px;">Tue</td>
<td style="width:50px;">Wed</td>
<td style="width:50px;">Thu</td>
<td style="width:50px;">Fri</td>
<td style="width:50px;">Sat</td>
</tr>
<?php
echo "<tr>";
for($i = 1; $i < $numDays+1; $i++, $counter++){
$timeStamp = strtotime("$year-$month-$i");
if($i == 1){
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++){
echo "<td> </td>";
}
}
if($counter % 7 == 0){
echo "</tr><tr>";
}
echo "<td align='center'>".$i."</td>";
}
echo "</tr>";
?>
</table>
</body>
</html>
Let's say today's month is January and I'm expecting when I select the next month it will go to February, not March.
You are correct that the problem stems from the available number of days in the next month.
Since you are using the current day as a base for $currentTimeStamp, you get unexpected results, not unlike those mentioned here: http://php.net/manual/en/function.strtotime.php#107331
If you want to keep using the current date in your calendar, you need to handle this problem somehow. I suggest that if today's date is larger than the number of days in the next month, you instead use the last day of that month:
$currentTimeStamp = strtotime("$year-$month");
if($day > date("t", $currentTimeStamp) {
$day = date("t", $currentTimeStamp);
}
$currentTimeStamp = strtotime("$year-$month-$day");