Selasa, 12 Juni 2012

Pemrograman Web PHP : Membuat Kalender dengan PHP

Karena suatu permintaan, terpaksa membuat sendiri kalender menggunakan PHP. Lumayan, compact, simpel dan mudah dipakai. Bisa ditambahkan untuk agenda dan lain-lain. Semoga bisa jalan dan bermanfaat. Oh ya, ada juga fungsi untuk "memaksa" PHP agar memakai suatu zona waktu, misal regional Indonesia, yaitu "Asia/Jakarta".

<?php
date_default_timezone_set("Asia/Jakarta");
$d=date('d');
$m=date('m');
$y=date('Y');
$nm=date('F');
$bln=$_GET['bln'];
$thn=$_GET['thn'];
if (($bln !="") && ($thn!=""))
{
$m=date('m',mktime(0,0,0,$bln,1,$thn));
$y=date('Y',mktime(0,0,0,$bln,1,$thn));
$nm=date('F',mktime(0,0,0,$bln,1,$thn));
}
$mbef=$m-1;
$maft=$m+1;
$nmmbef=date('M',mktime(0,0,0,$mbef,1,$thn));
$nmmaft=date('M',mktime(0,0,0,$maft,1,$thn));
$ybef=$y;
$yaft=$y;
if ($mbef<1) {$mbef=12; $ybef=$y-1;}
if ($maft>12) {$maft=1; $yaft=$y+1;}
$jmlkosong=date('w',mktime(0,0,0,$m,1,$y));
?>
<table summary="Calendar">
<caption>
<?php echo $nm;?> <?php echo $y?>
</caption>
<thead>
<tr>
<th abbr="Monday" scope="col" title="Monday">M</th>
<th abbr="Tuesday" scope="col" title="Tuesday">T</th>
<th abbr="Wednesday" scope="col" title="Wednesday">W</th>
<th abbr="Thursday" scope="col" title="Thursday">T</th>
<th abbr="Friday" scope="col" title="Friday">F</th>
<th abbr="Saturday" scope="col" title="Saturday">S</th>
<th abbr="Sunday" scope="col" title="Sunday">S</th>
</tr>
</thead>
<tfoot>
<tr>
<td abbr="October" colspan="3" id="prev"><a href="?bln=<?php echo $mbef;?>&thn=<?php echo $ybef;?>" title="">&laquo; <?php echo $nmmbef; ?></a></td>
<td class="pad">&nbsp;</td>
<td abbr="December" colspan="3" id="next"><a href="?bln=<?php echo $maft;?>&thn=<?php echo $yaft;?>" title=""><?php echo $nmmaft; ?> &raquo;</a></td>
</tr>
</tfoot>
<tbody>
  <?php
$jmlhari=date('t',mktime(0,0,0,$m,1,$y));
for ($i=1; $i<=$jmlkosong; $i++)
{
echo "<td>&nbsp;</td>";
}
$kolom=$jmlkosong;
for ($i=1; $i<=$jmlhari;$i++)
{
$kolom=$kolom+1;
$warna="#000000";
if ($kolom=='7') {$warna="#FF0000";}
if (($i==date('j')) && ($m==date('m')) && ($y==date('Y')))
{
$warna="#0000FF";
}
//mysql_connect("localhost","root","");
//mysql_select_db("toko_online");
//$q=mysql_query ("SELECT * FROM agenda WHERE DAY(tgl) = $i AND MONTH(tgl) = $m AND YEAR(tgl) = $y");
//echo "SELECT * FROM agenda WHERE DAY(tgl) = $i AND MONTH(tgl) = $m AND YEAR(tgl) = $y";
//$h=mysql_fetch_array ($q);
$clk="";
$cur="";
////if ($h['jenis']=='libur') {$warna = "#FF0000"; $clk="document.location='agenda.php?tgl=$i&bln=$m&thn=$y';"; $cur="cursor:pointer";}
//if ($h['jenis']=='meeting') {$warna = "#FFFF00"; $clk="document.location='agenda.php?tgl=$i&bln=$m&thn=$y';"; $cur="cursor:pointer";}
//if ($h['jenis']=='keluarga') {$warna = "#00FF00"; $clk="document.location='agenda.php?tgl=$i&bln=$m&thn=$y';"; $cur="cursor:pointer";}
?>
<td onClick="<?php echo $clk; ?>" style="<?php echo $cur; ?>"><font color="<?php echo $warna; ?>"><div align="center" class="style1"><?php echo $i; ?></div></font></td>
<?php
if ($kolom=='7')
{
echo '</tr><tr>';
$kolom=0;}
}
?>
</tbody>
</table>

Semoga berguna.