Jump to content
xisto Community
BuffaloHelp

Simple Php Calendar And Yahoo! Calendar Connection linking each date to Yahoo calendar

Recommended Posts

Original simple PHP calendar source here

 

I was looking for a simple PHP generated calendar. But I wanted to place detailed information to show on demand. And installing another database with separate management would have been too much of access codes and passwords to remember. And what if I want someone else to manage the calendar when I am not able? This is my version of using simple PHP calendar script to link to Yahoo! calendar.

 

The twist is that figuring out how Yahoo! calendar selection worked. And then depending on which date was clicked it will bring up the appropriate date/week on Yahoo! calendar.

 

First step was to understand the url.

When checking the status bar I noticed //calendar.mail.yahoo.com/Xisto.demo/?v=1&t=1157068800.

 

The values of v as follows:

v=0 : day view

v=1 : week view

v=2 : month view ...etc

 

The values of t as follows:

t values were assigned in total seconds in Unix Epoch time format starting from January 1, 1970. It took me about 8 hours to figure out how this worked. The logic I used was to figure out how to translate any given day number to unix seconds correctly. This will give an approximate time value for Yahoo! calendar to display correctly.

echo "<td bgcolor=#f2f2f2><a class='navigation' target='_blank' href='$yahoo/?v=0&t=$unixtime'> $day_num </a></td>\n";
This is the clickable link the script will generate using above values.

 

 

$clickdate = unixtojd(mktime(0,0,0,$month,$day_num,$year))
I found this code accidently that this will translate any $month, $day_num and $year set by the script. This was the hard part. Assign this to a variable.

 

$unixtime = jdtounix($clickdate);
Once a desired date's unix time was located, it must be re-translated back to Unix Epoch time format. Note that when you just use unixtojd, it will return time value that does not match to the right number of digits. It's because Unix Epoch counts seconds from january 1, 1970. So let's say you want to find Unix Epoch of September 1, 2006, you have to figure out the total second difference between these two dates :P Too much of work. But when unix time was found for julian date, you can then turn unix time to Unix Epoch value.

 

$yahoo = "http://calendar.mail.yahoo.com/Xisto.demo";
To make my code user friendly, I have assigned the link to Yahoo! calendar as a variable.

 

And the complete code goes like this. I modified from the original to insert a color

<?php//--my addition to Yahoo variables$yahoo = "http://calendar.mail.yahoo.com/Xisto.demo";//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);//Here we generate the first day of the month$first_day = mktime(0,0,0,$month, 1, $year);//This gets us the month name$title = date('F', $first_day);//Here we find out what day of the week the first day of the month falls on$day_of_week = date('D', $first_day);//Once we know what day of the week it falls on, we know how many blank days occure before it. //If the first day of the week is a Sunday then it would be zeroswitch($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;}//We then 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 headsecho "<table border=1 width=250>";echo "<tr><th colspan=7> $title $year </th></tr>";echo "<tr><td width=35 align=center>S</td><td width=35 align=center>M</td><td width=35 align=center>T</td><td width=35 align=center>W</td><td width=35 align=center>T</td><td width=35 align=center>F</td><td width=35 align=center>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 dayswhile ( $blank > 0 ){echo "<td></td>\n";$blank = $blank-1;$day_count++;}//sets the first day of the month to 1$day_num = 1;//count up the days, untill we've done all of them in the monthwhile ( $day_num <= $days_in_month ){//--my addition to Yahoo variables 2$clickdate = unixtojd(mktime(0,0,0,$month,$day_num,$year)); $unixtime = jdtounix($clickdate);echo "<td bgcolor=#f2f2f2><a class='navigation' target='_blank' href='$yahoo/?v=0&t=$unixtime'> $day_num </a></td>\n";$day_num++;$day_count++;//Make sure we start a new row every weekif ($day_count > 7){echo "</tr><tr>\n";$day_count = 1;}}//Finaly we finish out the table with some blank details if neededwhile ( $day_count >1 && $day_count <=7 ){echo "<td> </td>";$day_count++;}echo "</tr></table>";?>

Since this is a simple table generating sequence, it will obey all your CSS template :) Which is a very good thing. You can insert HREF class for the link.

 

See the working model here calendar

 

Next project modification idea - making hover effect to show different options, i.e. view in month/week/day. Give it a try... :P

 

**UPDATE**

Looks like instead of translating and re-translating you can simply use

$unixtime = mktime(0,0,0,$month,$day_num,$year);
now that I understand how mktime() works

Share this post


Link to post
Share on other sites

BH, get out of my head...Been working on a calendar script all week wondering how to make it a web-based, accessible calendar for the schedule of the local Hockey association.Perfect timing for this Tutorial.I think I might have an idea or two to add some color to this thing. I will post them here when I come up with them.

Share this post


Link to post
Share on other sites

Yes, the same unresulting searches lead me to develop this little trick myself. Looks like there aren't many sports related practical web scripts out there.

 

Hummm... a possible opportunity to make on goods?

 

And it wasn't that other PHP calendars weren't good enough. In fact EasyPHPCalendar was just super. But to use such over complexed script for few small sports leagues was not my intention. And to relate Yahoo! calendar (which what my teammates use anyway) already familiar with my team was the natural choice. Google calendar wasn't as easy as Yahoo! calendar to be utilized anyway. Yahoo! calendar also allows other Yahoo! users to add or modify one event calendar. This is useful when allowing multiple captains to have control over a season's league sessions.

 

jlhaslip, how about highlighting days i.e., all Tuesdays, Tuesday thur Saturday or just the working days.

 

**UPDATE**

 

I have made few adjustments to highlight or make HREF link appear only for certain days of the week. The new addition to the code

$shadeday = date('D' , $unixtime);if ($shadeday != "Sun" && $shadeday != "Mon" && $shadeday != "Sat")

By adding this simple condition, I can let my CLASS to be used only Tuesday thru Friday.

 

The modified code

<style type="text/css">
<!--@import url("css.css");
-->
</style>

<body>
Highlighting only certain days of the week<br /><br />

<?php
//--my addition to Yahoo variables
$yahoo = "http://calendar.mail.yahoo.com/Xisto.demo";

//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) ;

//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;

//This gets us the month name
$title = date('F', $first_day) ;

//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;

//Once we know what day of the week it falls on, we know how many blank days occure before it. 
//If the first day of the week is a Sunday then it would be zero
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;
}

//We then 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=250>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr>
<td width=35 align=center>S</td>
<td width=35 align=center>M</td>
<td width=35 align=center>T</td>
<td width=35 align=center>W</td>
<td width=35 align=center>T</td>
<td width=35 align=center>F</td>
<td width=35 align=center>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>\n";
$blank = $blank-1;
$day_count++;
}

//sets the first day of the month to 1
$day_num = 1;

//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{

//--my addition to Yahoo variables 2
$unixtime = mktime(0,0,0,$month,$day_num,$year);
$shadeday = date('D' , $unixtime);

if ($shadeday != "Sun" && $shadeday != "Mon" && $shadeday != "Sat") {
echo "<td><a class='navigation' target='_blank' href='$yahoo/?v=0&t=$unixtime'> $day_num </a></td>\n";
}
else {
echo "<td> $day_num </td>\n";
}
$day_num++;
$day_count++;

//Make sure we start a new row every week
if ($day_count > 7)
{
echo "</tr><tr>\n";
$day_count = 1;
}
}

//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}

echo "</tr></table>";

?>

</body>

 

You can view the working script here

Share this post


Link to post
Share on other sites

Oops. Just found one problem.Seems the date is tied to the server. It is 6:30 pm Mountain Daylight time on Sept 30th and when I clicked the calender demo link, it shows the October calender.

Share this post


Link to post
Share on other sites

Hummm yes you're right.

Is there a way to calculate viewer's time zone? Perhaps two options:
1) view in correct time zone according to a visitor
2) view in correct time zone according to event holder

After making this post, a quick search revealed

bool date_default_timezone_set ( string timezone_identifier )
Lists of timezone_identifier http://php.net/manual/en/timezones.php

Let's see if I can use this.

**UPDATE**

Modifying the initial time value, I was able to offset the server time and my time zone difference.

//This gets today's date$date =time () + (60 * 60 * -4);
This offsets -4 hours to reflect GMT-4. Since the value time() returns as Unix Epoch (in total seconds) I simply offset $date with 4 hours as units in seconds.

The next step is to see if this can be done using viewer's individual time zone.

Share this post


Link to post
Share on other sites

I have tested the time adjustment and it appears to work okay. It is set for gmt-4 right now.
I added some css and an if/else to 'shade' the rows so each week is clearly defined.
It still connects to your sample calendar.

http://forums.xisto.com/no_longer_exists/ to run the demo.

<style type="text/css">body {background-color: #dddddd;font: 0.85em Arial,Helvetica,Verdana, sans-serif, serif;min-height: 100%;margin-left:auto;margin-right:auto;text-align:center;}table, tr, td {	margin-left:auto;	margin-right:auto;	text-align:center;}a:link, a:visited {		color: green;		border-left: 5px solid #00ff00;		font-weight: bold;		display:block;		}a:hover	{		background-color: #00dd66;		color: #ffff00;		border-left: 5px solid #ffff00;		}		// use the classes odd and even for the colors of the shaded rows.odd   {background-color: #eeeeee;	    }.even   {background-color: #cccccc;	    }</style><body><h4>Highlighting only certain days of the week</br>And alternate shading of rows</h4><?php//--my addition to Yahoo variables$yahoo = "http://calendar.mail.yahoo.com/Xisto.demo";//This gets today's date$date =time () + (60 * 60 * -4);//This puts the day, month, and year in seperate variables$day = date('d', $date) ;$month = date('m', $date) ;$year = date('Y', $date) ;// use the row variable to switch shading on each line$row = 2;//Here we generate the first day of the month$first_day = mktime(0,0,0,$month, 1, $year) ;//This gets us the month name$title = date('F', $first_day) ;//Here we find out what day of the week the first day of the month falls on$day_of_week = date('D', $first_day) ;//Once we know what day of the week it falls on, we know how many blank days occure before it.//If the first day of the week is a Sunday then it would be zeroswitch($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;}//We then 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 headsecho "<table border=1 width=250>";echo "<tr class=\"even\"><th colspan=7> $title $year </th></tr>";echo "<tr class=\"odd\"><td width=35 align=center>S</td><td width=35 align=center>M</td><td width=35 align=center>T</td><td width=35 align=center>W</td><td width=35 align=center>T</td><td width=35 align=center>F</td><td width=35 align=center>S</td></tr>";//This counts the days in the week, up to 7$day_count = 1;echo "<tr class=\"even\">";//first we take care of those blank dayswhile ( $blank > 0 ){echo "<td></td>\n";$blank = $blank-1;$day_count++;}//sets the first day of the month to 1$day_num = 1;//count up the days, untill we've done all of them in the monthwhile ( $day_num <= $days_in_month ){//--my addition to Yahoo variables 2$unixtime = mktime(0,0,0,$month,$day_num,$year);$shadeday = date('D' , $unixtime);if ($shadeday != "Sun" && $shadeday != "Sat") {echo "<td><a class='$class' target='_blank' href='$yahoo/?v=0&t=$unixtime'> $day_num </a></td>\n";}else {echo "<td> $day_num </td>\n";}$day_num++;$day_count++;//Make sure we start a new row every weekif ($day_count > 7){	//check to see what css class is being used and switch classes	if ( $row == 1 ) {	   echo "</tr><tr class=\"even\">\n";	   $row = 2;	}	else {		echo "</tr><tr class= \"odd\">\n";	   $row = 1;	}$day_count = 1;}}//Finaly we finish out the table with some blank details if neededwhile ( $day_count >1 && $day_count <=7 ){echo "<td> </td>";$day_count++;}echo "</tr></table>";?></body>

Share this post


Link to post
Share on other sites

Couple of changes on this demo here.

http://forums.xisto.com/no_longer_exists/

CSS was altered.
Now links to my calendar. Adjusted to my time zone.
Added "add an event" button for web based changes by others IF your yahoo calendar allows them to modify the calendar. Add an individual user to your approved modifier list through Yahoo! calendar set up screen via your Yahoo! account.
Also, use the Yahoo! Calendar to make the calendar Public.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://forums.xisto.com/no_longer_exists/;  <title>Cal test 3</title>	<meta http-equiv="content-type"  content="text/html;charset=utf-8" >	<meta http-equiv="Content-Style-Type"  content="text/css" ><style type="text/css">body {	 background-color: #eeeeee;	 font: 0.85em Arial,Helvetica,Verdana, sans-serif, serif;	 min-height: 100%;	 margin-left:auto;	 margin-right:auto;	 text-align:center;}table, tr, td {	margin-left:auto;	margin-right:auto;	text-align:center;}a:link, a:visited {		color: #ff6666;		border-left: 3px solid #cc9966;		border-bottom: 3px solid #cc9966;		border-right: 3px solid #996633;		border-top: 3px solid #996633;		font-weight: bold;		display:block;		text-decoration:none;		}a:hover	{		background-color: #eecc33;		color: #996633;		border-left: 3px solid #996633;		border-bottom: 3px solid #996633;		border-right: 3px solid #cc9966;		border-top: 3px solid #cc9966;		}// use the classes odd and even for the colors of the shaded rows.odd   {background-color: #eeeeee;	    }.even   {background-color: #cccccc;	    } .highlight {background-color: #ddcccc;		   }a.highlight {background-color: #dd9999;			color:#666666;		   }a:hover.highlight {background-color: #ee6666;			color:#dddddd;		   }</style></head><body><h4>Linking all days of the month<br />Shading alternate weeks<br />Highlight of the current date</h4><?php//--my addition to Yahoo variables// var $yahoo defines the link which control is passed to$yahoo = "http://calendar.mail.yahoo.com/jlhaslip";// var $view defines the query string value to define the view used by $yahoo// uncomment the desired view// only one should be uncommented at a time$view = 0; // daily view//$view = 1; // weekly view //$view = 2; // monthly view //$view = 3; // yearly view //$view = 4; // daily view//$view = 5; // add event view //This gets today's date$date =time () + (60 * 60 * -7);//This puts the day, month, and year in seperate variables$day = date('d', $date) ;$month = date('m', $date) ;$year = date('Y', $date) ;// use the row variable to switch shading on each line$row = 2;//Here we generate the first day of the month$first_day = mktime(0,0,0,$month, 1, $year) ;//This gets us the month name$title = date('F', $first_day) ;//Here we find out what day of the week the first day of the month falls on$day_of_week = date('D', $first_day) ;//Once we know what day of the week it falls on, we know how many blank days occur before it.//If the first day of the week is a Sunday then it would be zeroswitch($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;}//We then 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 headsecho "<table border=1 width=200>";echo "<tr class=\"even\"><th colspan=7> $title $year </th></tr>";echo "<tr class=\"odd\"><td width=25 align=center>S</td><td width=25 align=center>M</td><td width=25 align=center>T</td><td width=25 align=center>W</td><td width=25 align=center>T</td><td width=25 align=center>F</td><td width=25 align=center>S</td></tr>";//This counts the days in the week, up to 7$day_count = 1;echo "<tr class=\"even\">";//first we take care of those blank dayswhile ( $blank > 0 ){	  echo "<td></td>\n";	  $blank = $blank-1;	  $day_count++;	  }//sets the first day of the month to 1$day_num = 1;//count up the days, untill we've done all of them in the monthwhile ($day_num <= $days_in_month ) {//--my addition to Yahoo variables 2$unixtime = mktime(0,0,0,$month,$day_num,$year);$shadeday = date('D' , $unixtime);//if ($shadeday != "Sun" && $shadeday != "Sat") {	if ( $day_num == $day) { 	   echo "<td><a class=\"highlight\" href='$yahoo/?v=$view&t=$unixtime'> $day_num </a></td>\n"; }	   else {	   		echo "<td><a href='$yahoo/?v=$view&t=$unixtime'> $day_num </a></td>\n";			   }//			}//			else {//				 echo "<td> $day_num </td>\n";//				 }$day_num++;$day_count++;//Make sure we start a new row every weekif ($day_count > 7) {//check var $row, switch classes / re-set $row  as requiredif ( $row == 1 ) {	   echo "</tr><tr class=\"even\">\n";	   $row = 0;	}	else {		echo "</tr><tr class= \"odd\">\n";	   $row = 1;	}$day_count = 1; }}//Finaly we finish out the table with some blank details if neededwhile ( $day_count >1 && $day_count <=7 ){	   	echo "<td> </td>";		$day_count++;		}echo "</tr></table><table><tr><td class='highlight'><a href='$yahoo/?v=5'>Click to add an event </a></td><tr>\n";echo "</table>";?></body>

Share this post


Link to post
Share on other sites

Well, I hate double posting, but here goes...

Still playing with this thing a little and here is what I have so far:

Version #9 used for testing - loops back to the php_self address
Header lists the filename, displays flag settings in footer
Test 9

Version #10
Full linking mode, no styling used
Test 10

Version #11
Full styling, Footer announcement, clickable
Full Header, no flags displayed.
Test 11

Hope you like them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.