How to add functionality to a simple php calender?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imsoconfused
Forum Newbie
Posts: 15
Joined: Mon Oct 05, 2009 10:55 pm

How to add functionality to a simple php calender?

Post by imsoconfused »

Hello everyone,

I have just finished creating a simple php calander. Can someone point me into the right direction as to what I need to do to begin to give the calender functionality?

For example
I want to make the days selectable

ultimately I am building a appointment/booking php script.. I don't want to pay for one I rather learn how to make my own.

thanks in advance,

imsoconfused,
sincerely
robnet
Forum Commoner
Posts: 85
Joined: Mon Aug 10, 2009 8:32 am
Location: South East, UK

Re: How to add functionality to a simple php calender?

Post by robnet »

You're going to have to provide a bit more information:

What does the calendar do at the moment? can you provide a summary and the code?

What functionality do you want?
imsoconfused
Forum Newbie
Posts: 15
Joined: Mon Oct 05, 2009 10:55 pm

Re: How to add functionality to a simple php calender?

Post by imsoconfused »

This is the code for the calender check out the next post for details

Code: Select all

<?php
 
//this gets today's date
$date = time();
 
//This puts the day/ month, and year in seperate variables.
$day = date('d', $date);
$month = date('m', $date);
$year = date('y', $date);
 
//Generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year);
 
//this gets the month name
$title = date('F', $first_day);
 
//TFind out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day);
 
//how many blanks fall before the first day starts in a month
switch($day_of_week){
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
case "Sat": $blank = 6; break;
}
 
//Determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year);
 
//Here we start building the table heads
echo "<table border=1 width=294>";
echo "<tr><th colspan=7>$title $year</th><tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td><td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td></tr>";
 
//This counts the days in the week, up to 7
$day_count = 1;
 
echo "<tr>";
//first we take care of those blank days
while($blank > 0)
{
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
 
//sets the first day of the month to 1
$day_num = 1;
 
//counts up the days until all days have been counted for that month
while($day_num <= $days_in_month)
{
echo "<td>$day_num</td>";
$day_num++;
$day_count++;
 
 
 
//make sure we start a new row every week
if($day_count > 7)
{
echo "</tr><tr>";
$day_count = 1;
}
}
 
while($day_count >1 && $day_count <=7)
{
echo "<td></td>";
$day_count++;
}
echo "</tr></table>";
 
 
 
 
?>
imsoconfused
Forum Newbie
Posts: 15
Joined: Mon Oct 05, 2009 10:55 pm

Re: How to add functionality to a simple php calender?

Post by imsoconfused »

The main thing I am concerned about is to change the 31 calender days from static boxes to clickable fields that will send out a date for example. the user clicks on day 6 so the script will spit out October 6 2009. Basically a Date picker.

Should I convert the table to div ul li and use a? or can this be done with the table?

thanks in advance,

imsoconfused,
sincerely
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: How to add functionality to a simple php calender?

Post by chopsmith »

You'll have to write some Javascript. If you want to do it simply, without AJAX or anything, do the following:

Change your line 57 from

Code: Select all

 
echo "<td>$day_num</td>";
 
to

Code: Select all

 
echo "<td><a href='javascript&#058;putDateInTextField(" . $year . "," . $month . "," . $day_num . ")'>$day_num</a></td>";  //I see that devnetwork converted a colon into & # 058 ; (without the spaces).  Your code should have a colon between javascript and putDateInTextField
 
That will turn your day number into links, and when a link is clicked, a javascript function called putDateInTextField() will be called, and it will be passed the year, month, and day number.

Then, in the <head></head> section of your html, place a function like this:

Code: Select all

 
function putDateInTextField(year,month,day_num) {
    date = month + "/" + day_num + "/" + year; //formats the date to be put in text box
    document.getElementById("dateTextBox").value = date; //puts the formatted date into a text box to which you have assigned the id="dateTextBox"
    return true;
}
 
That's basically all you have to do, although there are caveats related to browser-compatibility, algorithm, etc. For example, this assumes that each date in the calander is always going to be for the year and month of today's date. Also note that I haven't tested your code or the code I've written above, but it's pretty straightforward and I think it should work. Might need a tweak or two.
Post Reply