29 Nov 2012
create simple php calendar
php calendar is one of the important think in web programming, It will be helpful in many occations Like online booking, many online translation ect.
If you are dealing with php websites there may be many occasion that you may need php calendar.
Let us check how we can create simple php calendar.
What I’m going to show now is creating a php calendar using php codes and html
i’m using inbuilt php date functions to create the dates.
Lets enter into coding.
<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
if (!isset($_GET["m"])) $_GET["m"] = date("n");
if (!isset($_GET["y"])) $_GET["y"] = date("Y");
$currentMonth = $_GET["m"];
$currentYear = $_GET["y"];
$p_year = $currentYear;
$n_year = $currentYear;
$p_month = $currentMonth-1;
$n_month = $currentMonth+1;
if ($p_month == 0 ) {
$p_month = 12;
$p_year = $currentYear - 1;
}
if ($n_month == 13 ) {
$n_month = 1;
$n_year = $currentYear + 1;
}
$days=array('1'=>"S",'2'=>"M",'3'=>"T",'4'=>"W",'5'=>"T",'6'=>"F",'7'=>"S");
?>
<table width="800">
<tr align="center">
<td bgcolor="#7DC3E3" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?m=". $p_month . "&y=" . $p_year; ?>" style="color:#FFFFFF">Prev</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?m=". $n_month . "&y=" . $n_year; ?>" style="color:#FFFFFF">Next</a> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="1" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#7DC3E3" style="color:#FFFFFF"><B><?php echo $monthNames[$currentMonth-1].' '.$currentYear; ?></B></td>
</tr>
<tr >
<?php for($i=1;$i<=7;$i++){ ?>
<td align="center" height='100' bgcolor="#7DC3E3" style="color:#FFFFFF"><B><?php echo $days[$i]; ?></B></td>
<?php } ?>
</tr>
<?php
$timestamp = mktime(0,0,0,$currentMonth,1,$currentYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ) echo "<tr>";
if($i < $startday) echo "<td ></td>";
else echo "<td align='center' height='80' valign='middle' bgcolor='#AED5E4' height='20px'>". ($i - $startday + 1) . "</td>";
if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>
</td>
</tr>
</table>

Hi, youre work is beautiful but I have a problem. In my country we have a start day monday. If I change start day for monday, then february have 31 days. How I can repair it pls?
Sucenek
@Sucenek
I think in your case you can put a small hack here by putting
just above the beginning of the for loop.
Hope this will work for you.
you can also put
$days=array('1'=>"M",'2'=>"T",'3'=>"W",'4'=>"T",'5'=>"F",'6'=>"S",'7'=>"S");[php]so it will start Monday as starting day
$days=array(’1′=>”M”,’2′=>”T”,’3′=>”W”,’4′=>”T”,’5′=>”F”,’6′=>”S”,’7′=>”S”); <This I have
$startday=$startday-1; <- It is not working (or where I do insert it?)
$timestamp = mktime(0,0,0,$currentMonth,0,$currentYear); <- 1st January 2013 start in Tuesday and here I chaned it, but February still have a 31 days.
If I change it back for : $currentMonth,1,$currentYear <- Then 1st January start at Wednesday but February have 28 days…
put this code
$timestamp = mktime(0,0,0,$currentMonth,1,$currentYear); $maxday = date("t",$timestamp); $thismonth = getdate ($timestamp); $startday = $thismonth['wday']; // new modified code $startday=$startday-1; for ($i=0; $i<($maxday+$startday); $i++) { if(($i % 7) == 0 ) echo "<tr>"; if($i < $startday) echo "<td ></td>"; else echo "<td align='center' height='80' valign='middle' bgcolor='#AED5E4' height='20px'>". ($i - $startday + 1) . "</td>"; if(($i % 7) == 6 ) echo "</tr>"; }It work. Thank you so much ! Now I must do from it a event clanedar. It will be maybe little more difficult. Thank you
Sucenek
Ok. Good luck for event calendar..
I have use the code, i want to modified it. Please help me to do this:
1. The “sunday” column use the red background
2. The today date use the bold color
Please help me. Thanks
nael please try this code this may helpful to you to create simple php calendar with The “sunday” column use the red background and The today date use the bold color.
<?php $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); if (!isset($_REQUEST["m"])) $_REQUEST["m"] = date("n"); if (!isset($_REQUEST["y"])) $_REQUEST["y"] = date("Y"); $currentMonth = $_REQUEST["m"]; $currentYear = $_REQUEST["y"]; $p_year = $currentYear; $n_year = $currentYear; $p_month = $currentMonth-1; $n_month = $currentMonth+1; if ($p_month == 0 ) { $p_month = 12; $p_year = $currentYear - 1; } if ($n_month == 13 ) { $n_month = 1; $n_year = $currentYear + 1; } $days=array('1'=>"S",'2'=>"M",'3'=>"T",'4'=>"W",'5'=>"T",'6'=>"F",'7'=>"S"); $todays_date= date(j); $todays_month= date(n); ?> <table width="800"> <tr align="center"> <td bgcolor="#7DC3E3" style="color:#FFFFFF"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="50%" align="left"> <a href="<?php echo $_SERVER["PHP_SELF"] . "?m=". $p_month . "&y=" . $p_year; ?>" style="color:#FFFFFF">Prev</a></td> <td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"] . "?m=". $n_month . "&y=" . $n_year; ?>" style="color:#FFFFFF">Next</a> </td> </tr> </table> </td> </tr> <tr> <td align="center"> <table width="100%" border="1" cellpadding="2" cellspacing="2"> <tr align="center"> <td colspan="7" bgcolor="#7DC3E3" style="color:#FFFFFF"><B><?php echo $monthNames[$currentMonth-1].' '.$currentYear; ?></B></td> </tr> <tr > <?php for($i=1;$i<=7;$i++){ if($i==1){ $c="#FF0000";} else { $c="#7DC3E3"; } ?> <td align="center" height='100' bgcolor="<?php echo $c; ?>" style="color:#FFFFFF"><B><?php echo $days[$i]; ?></B></td> <?php } ?> </tr> <?php $timestamp = mktime(0,0,0,$currentMonth,1,$currentYear); $maxday = date("t",$timestamp); $thismonth = getdate ($timestamp); $startday = $thismonth['wday']; for ($i=0; $i<($maxday+$startday); $i++) { if(($i % 7) == 0){ $c="#FF0000"; } else { $c="#7DC3E3"; } if(($todays_date==($i - $startday + 1))&&($todays_month==$currentMonth)){ $fw='bold'; $fs='30px'; } else{ $fw='normal'; $fs='16px'; } if(($i % 7) == 0 ) echo "<tr>"; if($i < $startday) echo "<td ></td>"; else echo "<td align='center' style='font-weight:$fw;font-size:$fs' height='80' valign='middle' bgcolor='$c' height='20px'>". ($i - $startday + 1) . "</td>"; if(($i % 7) == 6 ) echo "</tr>"; } ?> </table> </td> </tr> </table>