Skip to content

Instantly share code, notes, and snippets.

@mazhar266
Created October 15, 2014 10:51
Show Gist options
  • Save mazhar266/9512e88a3adee3221ce5 to your computer and use it in GitHub Desktop.
Save mazhar266/9512e88a3adee3221ce5 to your computer and use it in GitHub Desktop.
Calendar in PHP
<?php
function year2array ($year)
{
$res = $year >= 1970;
if ($res)
{
// this line gets and sets same timezone, don't ask why :)
date_default_timezone_set ( date_default_timezone_get () );
$dt = strtotime ( "-1 day", strtotime ( "$year-01-01 00:00:00" ) );
$res = array ();
$week = array_fill ( 1, 7, false );
$last_month = 1;
$w = 1;
do {
$dt = strtotime ( '+1 day', $dt );
$dta = getdate ( $dt );
$wday = $dta ['wday'] == 0 ? 7 : $dta ['wday'];
if (($dta ['mon'] != $last_month) || ($wday == 1))
{
if ($week [1] || $week [7])
$res [$last_month] [] = $week;
$week = array_fill ( 1, 7, false );
$last_month = $dta ['mon'];
}
$week [$wday] = $dta ['mday'];
} while ( $dta ['year'] == $year );
}
return $res;
}
function month2table($month, $calendar_array)
{
$ca = 'align="center"';
$res = "<table cellpadding=\"2\" cellspacing=\"1\" style=\"border:solid 1px #000000;font-family:tahoma;font-size:12px;background-color:#ababab\"><tr><td $ca>সোম</td><td $ca>মঙ্গল</td><td $ca>বুধ</td><td $ca>বৃহস্পতি</td><td $ca>শুক্র</td><td $ca>শনি</td><td $ca>রবি</td></tr>";
foreach ( $calendar_array [$month] as $month => $week )
{
$res .= '<tr>';
foreach ( $week as $day )
{
$res .= '<td align="right" width="20" bgcolor="#ffffff">' . ($day ? $day : '&nbsp;') . '</td>';
}
$res .= '</tr>';
}
$res .= '</table>';
return $res;
}
$calarr = year2array ( 2014 );
echo month2table ( 10, $calarr ); // January
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment