Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Php.

I am coding a quite huge calendar script for various works I have in progress.
The barebones of this calendar I have coded today represent a php function that returns a calendar into an html table of any month of any year (according to php limits) with any day offset.
This means your calendar could start on sunday, monday, tuesday and so on.
It’s just the beginning but you can customize it with styles and other funny things.

Let’s see the code.

function calendar($year, $month, $day_offset = 0){ 
    $days = array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
    $months = array("january","february","march","april","may","june","july","august","september","october","november","december");
    $day_offset = $day_offset % 7;
    $start_day = gmmktime(0,0,0,$month,1,$year); 
    $start_day_number = date("w",$start_day);
    $days_in_month = date("t",$start_day);
    $final_html .= "\n\n";
    for($x=0;$x<=6;$x++){
    	$final_html .= "";
    }
    $final_html .= "\n";
    $blank_days = $start_day_number - $day_offset;
    if($blank_days<0){$blank_days = 7-abs($blank_days);}
    for($x=0;$x<$blank_days;$x++){
    	$final_html .= "";
    }
    for($x=1;$x<=$days_in_month;$x++){
    	if(($x+$blank_days-1)%7==0){
    		$final_html .= "\n";
    	}
    	$final_html .= "";
    }
    while((($days_in_month+$blank_days)%7)!=0){
    	$final_html .= "";
    	$days_in_month++;
    }
    $final_html .= "\n
".$months[$month-1]." $year
".$days[($x+$day_offset)%7]."
x
$xx
"; return($final_html); }

Since it’s just the beginning the code is quite simple, but if you have any question or suggestion, just leave a comment.

How will you use this function?

If you want the March 2006 calendar starting on Sunday, just

echo calendar(2006,3);

and the result is

\n

\n”;
for($x=0;$x<=6;$x++){ $final_html .= "

“;
}
$final_html .= “

\n”;
$blank_days = $start_day_number – $day_offset;
if($blank_days<0){$blank_days = 7-abs($blank_days);} for($x=0;$x<$blank_days;$x++){ $final_html .= "

“;
}
for($x=1;$x<=$days_in_month;$x++){ if(($x+$blank_days-1)%7==0){ $final_html .= "

\n

“;
}
$final_html .= “

“;
}
while((($days_in_month+$blank_days)%7)!=0){
$final_html .= “

“;
$days_in_month++;
}
$final_html .= “

\n

“.$months[$month-1].” $year
“.$days[($x+$day_offset)%7].”
x
$x x

“;
return($final_html);
}

echo calendar(2006,3);

?>

While if you want April 2006 starting from monday, just

echo calendar(2006,4,1);

and the result is

GRAHAM Sent me a code he modified to include cycling through months and years and converted it to div’s making it xhtml compliant.

Here it is





Calender



  • Back".$months[$month-1]." $yearNext
  • \n"; $final_html .= "
  • "; for($x=0;$x<=6;$x++){ $final_html .= "".$days[($x+$day_offset)%7].""; } $final_html .= "
  • \n"; $final_html .= "
    • \n"; $blank_days = $start_day_number - $day_offset; if($blank_days<0){$blank_days = 7-abs($blank_days);} for($x=0;$x<$blank_days;$x++){ $final_html .= " "; } for($x=1;$x<=$days_in_month;$x++){ if(($x+$blank_days-1)%7==0){ $final_html .= "
    • \n
    • "; } $final_html .= "$x"; } while((($days_in_month+$blank_days)%7)!=0){ $final_html .= " "; $days_in_month++; } $final_html .= "
    • \n
    "; return($final_html); } if(isset($_GET['y'])){ $y = $_GET['y']; } else { $y = date("Y"); } if(isset($_GET['m'])){ $m = $_GET['m']; } else { $m = date("m"); } echo calendar($y,$m); ?>

    Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.