user1236784
user1236784

Reputation: 103

PHP calendar skips February

I have looked and haven't found an answer.

I have a php calendar and when going to next month or previous month fro jan to feb it skips feb and vise versa from march to feb it skips feb. It does this every 4 years so I know it has to do with leap year but can't seem to find problem.

here is the code:

<html>
<head>
<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"); 
}

// calender variable // 
$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' align='center'> <?php echo $monthName.", ".$year; ?></td>
<td><input style='width:50px;' type='button' value='>' name='nextbutton' onclick="goNextMonth(<?php echo $month.",".$year?>)"></td>
<td></td> 
</tr>
<tr>
<td width='50px' align='center'>D</td>
<td width='50px' align='center'>L</td>
<td width='50px' align='center'>M</td>
<td width='50px' align='center'>M</td>
<td width='50px' align='center'>J</td>
<td width='50px' align='center'>V</td>
<td width='50px' align='center'>S</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++) {
// blank space //
echo "<td>&nbsp;</td>";
} 
}
if ($counter % 7 == 0 && $counter != 0){
echo "</tr><tr>";
}
echo "<td align='center'>".$i."</td>";
}
echo "</tr>";
?>

</table>

</body>

Upvotes: 0

Views: 238

Answers (2)

deceze
deceze

Reputation: 522382

if (isset($_GET['day'])) {
    $day = $_GET['day'];
} else { 
    $day = date("j");
}

...

$currentTimeStamp = strtotime("$year-$month-$day");

If no explicit day is given, you're taking the current day. Guess what, today is the 30th. There is no February 30th. You're then basing all your date calculations on this.

If you want to make a timestamp for a certain month and then iterate through to the next month, always take the beginning of the month. January 1st + 1 month is meaningful, January 30th + 1 month is not.

Upvotes: 0

Mooseknuckles
Mooseknuckles

Reputation: 497

I suspect your problem is that Javascript treats January as month 0 while PHP treats January as month 1.

I feel like you've flipped this.

Are you getting your month from javascript or PHP? If javascript, you should never have month 12. In either case, you should never have month 13.

Since you said month rather than $month I'm going to assume it's coming from Javascript. In that case, I think I would change it like this:

function goLastMonth(month, year){
    if (month == 0) {
        --year;
        month = 11;
}

...

function goNextMonth(month, year){
    if (month == 11) {
        ++year;
        month = 0;
}

...

Upvotes: 1

Related Questions