Jump to content

[SOLVED] Calendar using PHP?


denhamd2

Recommended Posts

Hi guys,

 

Is it possible to make a calendar in PHP rather than hard-coding it in HTML? For example, I'm looking to make one for June 2007 and on each line I'd like to have the day, the date and then a checkbox containing the date, so for example:

 

<input type="checkbox" name="date" value="01062007" /> Tues. 1st<br>

 

Is it possible to do this dynamically using PHP?

 

Thanks in advance

Link to comment
Share on other sites

Simple function, no, but here's a function that you can modify to your specifications. It allows you to pass two optional parameters through: one for year and one for month. It then generates and displays a calendar with days and checkboxes for each day. Use it as you will!

<?php
function datePicker($year = '', $month = '') {
$year   = empty($year) ? date('Y') : $year;
$month  = empty($month) ? date('m') : $month;
$ts     = mktime(0,0,0,$month,1,$year);
$dInMo  = date('t', $ts);
$offset = date('w', $ts);
$weeks  = ceil(($offset + $dInMo) / 7);
$mName  = date('F Y', $ts);

$wDay = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa');
$out  = "<table id=\"myCalendar\">\n";
$out .= "<tr>\n";
$out .= "<th colspan=\"7\">$mName</th>\n";
$out .= "</tr>\n";
$out .= "<tr>\n";
$out .= "<td>" . implode("</td>\n<td>", $wDay) . "</td>\n";
$out .= "</tr>\n";

$d = 1 - $offset; // Define your starting day
// Loop your weeks
for ($i = 0; $i < $weeks; $i++) {
	$out .= "<tr>\n";	
	// Loop 7 days for week
	for ($x = 0; $x < 7; $x++) {
		$out .= "<td>\n";
		if ($d > 0 && $d <= $dInMo) {
			$dTs  = mktime(0,0,0,$month, $d, $year);
			$out .= "<div class=\"dayValue\">$d</div>\n";
			$out .= "<div class=\"checkValue\"><input type=\"checkbox\" name=\"myDates[]\" value=\"$dTs\" /></div>\n";
		} else {
			$out .= " ";
		}

		++$d;
		$out .= "</td>\n";
	}
	$out .= "</tr>\n";
}

$out .= "</table>\n";

return $out;
}

// Usage:
echo datePicker(2007, 10);
?>

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.