Web Programming  Home Web Programming PHP Make a Calendar
rss

Make a Calendar

Author: Will More by this author


Make a CalendarNote: you must have calendar support enabled on php for this to work. you can check this with phpinfo();This tutorial was last edited Sunday 19th February

Making a calendar in PHP is quite simple, you just need the right logic.

We'll start off with declaring our variables and starting off the function.

function createCalendar() {

$calendar = "CAL_GREGORIAN"; // Sets the "int" argument for the calendar functions.

$year = date("Y"); // This year

$unix = time(); // Now

$today = unixtojd($unix); // Converts into Julian

$today = cal_from_jd($today, CAL_GREGORIAN); // Converts into normal (i.e. weekday)

The comments should help you out. Now that we've done that, we're going to start the output.

echo "<table cellpadding="3" cellspacing="0">";

echo "<tr><td class="headdate" colspan="33" align="left"><b>Welcome, {$_SERVER['REMOTE_ADDR']}. | Today is ".$today[date]."</b></th></td>";

And now we finish off the output:

$months = array(
"January" => 1,
"February" => 2,
"March" => 3,
"April" => 4,
"May" => 5,
"June" => 6,
"July" => 7,
"August" => 8,
"September" => 9,
"October" => 10,
"November" => 11,
"December" => 12
); // Sets up the months into an associative array.

$r = 0;

foreach($months as $key => $value) {

$days_in_month = cal_days_in_month(CAL_GREGORIAN,$value,$year); // How many days there are in the month

echo "<tr style=""; // The next few lines are simply for aesthetics.
if($key==date("F")) {
echo " background: #239487;";
}
echo "cursor: default;"";
if($r==1) {
echo " bgcolor="#CFCFCF"";
$r = 0;
}
else {
$r++;
}
echo "><th><b>$key $year</b></th>n<td>";

for($i=0;$i<$days_in_month;$i++) {

$week_number = date("Week W",mktime(0, 0, 0, $value, ($i+1), $year)); // Finds out the week of the year

$weekday = date("l",mktime(0, 0, 0, $value, ($i+1), $year)); // What weekday it is today

echo "<td title="$week_number: $weekday"><a href="?">";

if($weekday==date("l") & & $i == (date("j")-1)) {
echo "<!-- This is today -->";
}
if($weekday=="Saturday"||$weekday=="Sunday") { // Is it a weekend?
echo "<b>".($i+1)."</b> ";
}
else {
echo ($i+1)."</a></td>n";
}

}



}

echo "</td></tr>";

echo "</table>";

}

The full function:

function createCalendar() {

$calendar = "CAL_GREGORIAN"; // Sets the "int" argument for the calendar functions.

$year = date("Y"); // This year

$unix = time(); // Now

$today = unixtojd($unix); // Converts into Julian

$today = cal_from_jd($today, CAL_GREGORIAN); // Converts into normal (i.e. weekday)

echo "<table cellpadding="3" cellspacing="0">";

echo "<tr><td class="headdate" colspan="33" align="left"><b>Welcome, {$_SERVER['REMOTE_ADDR']}. | Today is ".$today[date]."</b></th></td>";

$months = array(
"January" => 1,
"February" => 2,
"March" => 3,
"April" => 4,
"May" => 5,
"June" => 6,
"July" => 7,
"August" => 8,
"September" => 9,
"October" => 10,
"November" => 11,
"December" => 12
); // Sets up the months into an associative array.

$r = 0;

foreach($months as $key => $value) {

$days_in_month = cal_days_in_month(CAL_GREGORIAN,$value,$year); // How many days there are in the month

echo "<tr style=""; // The next few lines are simply for aesthetics.
if($key==date("F")) {
echo " background: #239487;";
}
echo "cursor: default;"";
if($r==1) {
echo " bgcolor="#CFCFCF"";
$r = 0;
}
else {
$r++;
}
echo "><th><b>$key $year</b></th>n<td>";

for($i=0;$i<$days_in_month;$i++) {

$week_number = date("Week W",mktime(0, 0, 0, $value, ($i+1), $year)); // Finds out the week of the year

$weekday = date("l",mktime(0, 0, 0, $value, ($i+1), $year)); // What weekday it is today

echo "<td title="$week_number: $weekday"><a href="?">";

if($weekday==date("l") & & $i == (date("j")-1)) {
echo "<!-- This is today -->";
}
if($weekday=="Saturday"||$weekday=="Sunday") { // Is it a weekend?
echo "<b>".($i+1)."</b> ";
}
else {
echo ($i+1)."</a></td>n";
}

}



}

echo "</td></tr>";

echo "</table>";

}


Author's URL: www.avengex.com

Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Make a Calendar"