Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Simple Php Calendar And Yahoo! Calendar Connection, linking each date to Yahoo! calendar
BuffaloHELP
post Sep 30 2006, 10:33 AM
Post #1


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,478
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



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/trap17.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.
CODE
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.


CODE
$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.

CODE
$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 tongue.gif Too much of work. But when unix time was found for julian date, you can then turn unix time to Unix Epoch value.

CODE
$yahoo = "http://calendar.mail.yahoo.com/trap17.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
CODE
<?php
//--my addition to Yahoo variables
$yahoo = "http://calendar.mail.yahoo.com/trap17.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
$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 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>";

?>


Since this is a simple table generating sequence, it will obey all your CSS template smile.gif 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... wink.gif

**UPDATE**
Looks like instead of translating and re-translating you can simply use
CODE
$unixtime = mktime(0,0,0,$month,$day_num,$year);

now that I understand how mktime() works
Go to the top of the page
 
+Quote Post
jlhaslip
post Sep 30 2006, 02:21 PM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,077
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
BuffaloHELP
post Sep 30 2006, 08:33 PM
Post #3


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,478
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



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
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
HTML
<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/trap17.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
Go to the top of the page
 
+Quote Post
jlhaslip
post Oct 1 2006, 12:29 AM
Post #4


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,077
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



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.
Go to the top of the page
 
+Quote Post
BuffaloHELP
post Oct 1 2006, 12:41 AM
Post #5


Desperately seeking "any key" to continue...
Group Icon

Group: Admin
Posts: 3,478
Joined: 23-April 05
From: Trap17 storage box
Member No.: 6,042



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
CODE
bool date_default_timezone_set ( string timezone_identifier )

Lists of timezone_identifier http://www.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.

CODE
//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.
Go to the top of the page
 
+Quote Post
jlhaslip
post Oct 1 2006, 04:35 AM
Post #6


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,077
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



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://www.jlhaslip.trap17.com/cal_test/test_cal.php to run the demo.

CODE

<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/trap17.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 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 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 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 != "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 week
if ($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 needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}

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

?>

</body>
Go to the top of the page
 
+Quote Post
jlhaslip
post Oct 3 2006, 06:40 AM
Post #7


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,077
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



Couple of changes on this demo here.

http://www.jlhaslip.trap17.com/cal_test/test3.php

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.
CODE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<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