Link to home
Create AccountLog in
Avatar of NeoAshura
NeoAshuraFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Php Calendar / Outlook question

Hi experts,

I found this code on the internet and was wondering how to modify it to be used with our companies outlook calendar, It would be good if the user at the company could click the link to the calendar page and it returns the calendar events for that week if possible.

Here is the code i found.

The data would be taken from a mysql database but would like it linking with outlook if possible.

Many thanks for your time.

code is attached.


<?php
//SET THE TIMEZONE
$success = date_default_timezone_set("America/Denver");
header("Content-Type: text/Calendar");
header("Content-Disposition: inline; filename=calendar.ics");
echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "PRODID:-//Generated by PHP in Linux!//NONSGML Linux Rocks//EN\n";
echo "METHOD:REQUEST\n"; // requied by Outlook
echo "BEGIN:VEVENT\n";
echo "UID:".date('Ymd').'T'.date('His')."-".rand()."-example.com\n"; // required by Outlok
echo "DTSTAMP:".date('Ymd').'T'.date('His')."\n"; // required by Outlook
echo "DTSTART:$_GET[date]\n"; 
echo "SUMMARY:Visit with $_GET[contact]\n";
echo "DESCRIPTION: Visit with $_GET[contact] at $_GET[customer]\n";
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of NeoAshura

ASKER

Is there anyway of returning a whole week? with the date laid out in calendar format?

I have no experience what so ever in doing this so wouldn't even know where to start.

Hopefully your knowledge and advice will be able to help me in the right direction.

Also do you have any resources/Examples/Tutorials of what i want being done? in relation to returning the calendar information.

also am i right in thinking that you would be able to link the calendar to the database? so it dynamically fills the calendar with the contracts ending in say that week or within 7 days from the current date?

Many thanks for your help it is greatly appreciated as always.
I've never tried sending more than one calendar element in a vCal file.  Also I've never tried to cause a download of more than one file per client click.  So I guess I would be in research mode on this matter, too.

This is really old code and may be fairly brittle, but it has worked OK for years.  

Also, I have an article here with some good information on handling DATETIME fields.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

HTH, ~Ray
<?php // calendar_vcal.php

require_once('_config.php');
// access_control();

error_reporting(E_ALL ^ E_NOTICE);


$_key		= $_GET["k"];
if (!integer_string($_key)) die("Bad Key");

$filename = "LBBEvent" . $_key . ".vcs";
header("Content-Type: text/x-vCalendar");
header("Content-Disposition: inline; filename=$filename");


$sql 	= "SELECT * FROM calendar ";
$sql	.= "WHERE _key = $_key LIMIT 1 ";
if (!$res = mysql_query($sql)) { query_error($sql); }
$num_rows = mysql_num_rows($res);

if ($num_rows == 0) {
	echo "Sorry, the <b>Event Key $_key</b> is not found in the calendar.<br /><br />\n";
	echo "Please report this error to the curator.<br /><br />\n";
	die("Sorry, Unable to add to your calendar.");
}
if ($num_rows != 1) {
	echo "SQL $sql<br>";
	echo "RESULT $result<br><br>";
	echo "The <b>Event Key $_key</b> appears to have $num_rows data base entries in the calendar (yikes!)<br />\n";
 	echo "Please report this error to the curator.<br /><br />\n";
 	die("Sorry, Unable to add to your calendar.");
}

$calendar_array		= mysql_fetch_assoc($res); // NOTE WE DO NOT TRANSFER THE CATEGORY TO THE V-CAL

foreach ($calendar_array as $k => $v) { $$k = $v; }

$loc					= trim($location);
if ($loc  != '') { $loc  = '=0D=0A=Location: ' . $loc; }

$poc1					= trim($poc1_name .' '. $poc1_phone .' '. $poc1_email);
if ($poc1 != '') { $poc1 = '=0D=0A=Contact: ' . $poc1; }

$poc2					= trim($poc2_name .' '. $poc2_phone .' '. $poc2_email);
if ($poc2 != '') { $poc2 = '=0D=0A=Contact: ' . $poc2; }

$pocw					= trim($poc_url);
if ($pocw != '') { $pocw = '=0D=0A=WWW: ' . $pocw; }

$poc = $poc1 . $poc2 . $pocw;

// CALENDAR TOLERATES END TIMESTAMPS WRONG OR OMITTED - VCAL DOES NOT
$vcal_dtstart = date('Y-m-d\TH:i:sO', strtotime($start));
$vcal_dtend   = date('Y-m-d\TH:i:sO', strtotime($end));
if ($vcal_dtend < $vcal_dtstart) { $vcal_dtend = $vcal_dtstart; }


?>BEGIN:VCALENDAR
VERSION:1.0
PRODID:LBB Online Calendar
BEGIN:VEVENT
SUMMARY:<?php echo "$title" . "\n"; ?>
DESCRIPTION;ENCODING=QUOTED-PRINTABLE:<?php echo "$details" . "$loc" . "$poc" . "\n"; ?>
DTSTART:<?php echo $vcal_dtstart . "\n"; ?>
DTEND:<?php echo $vcal_dtend . "\n"; ?>
END:VEVENT
END:VCALENDAR

Open in new window

Can i ask what your code actually does?

I see that it pulls data from the calendar table, But what type of information does it pull, how is it displayed, and if for example  i had a table called "customer" and in that field i had "contract_end" would this suffice for seeing the next 7 days and who would be coming to the end of their contract?

Perhaps a screen shot of what it also looks like would benefit me.

I completely appreciate your help.
also where is K passed from i noticed there was a "GET"
$_GET is the superglobal array that contains the URL arguments.  Example:
http://laprbass.com/RAY_bounce_get.php?foo=bar

That program script will find the variable $_GET["foo"] contains the string "bar."  In this case, the GET argument is the event number in the calendar table.

The code snippet above generates a vCal file from information in the calendar table.  It is vCal 1.0 - very elementary and uncomplicated.  Look at the headers on line 13 and 14, then look at the output data string at line 59 where the program drops out of PHP.  The PHP vars include the title, description, start and end.  The description is made up from information in the data base, including the details of the event, the points of contact, the location, etc.

No screen shot is available because all this does is trigger the calendar program on the client computer (probably Outlook in Windows, for example) to add an event to the client calendar.  It works on all Apple Mac systems that I have tested, on Google calendars, etc.  Any standards-based calendar program that is associated with the ".vcs" file type should be OK.  Learn more here:
http://www.imc.org/pdi/vcal-10.txt

No calendar application has ever been as good as the paper version of the calendar it seeks to replace.  

A minor cautionary tale about calendars.  Not too long after we started using this calendar on a school web site (1999) the headmaster got his knickers in a knot over the fact that each calendar event had to be clicked on once so that it could be downloaded to his Outlook calendar.  He wanted all of them to be automatically downloaded at a single click.  On the face of it, that would not seem unreasonable, even in 1999.  To give you an idea of the workload involved, we had maybe 35 calendar data elements for each school year.  Thus it would have taken 35 mouse clicks.  If you listen, you can almost hear him saying, "This is totally unacceptable!"  

So he hired a "technology expert" who talked him into buying a huge javascript based application.  The technology expert is still on staff, at a recurring cost of more than $70,000 plus benefits per year, the javascript application is still installed and is costing them $2,000 per year.  Usage has dwindled; hardly anyone uses it any more.  The headmaster is gone, but the prevailing sentiment is, "We could not live without our technology expert."  In other words, the headmaster made a choice not to click 35 links; instead he chose to institutionalize a sunk cost that has now ballooned to nearly a million dollars over the last decade.  Oh, and by the way, the javascript application cannot talk to web browsers in a way that enables you to click a link and download a calendar entry -- it has no understanding of file association.  It requires a separate application to be installed on all the client computers.  And it only supports Windows, so the art teacher's Mac cannot use the calendar (she uses a paper calendar, which hangs on the wall where everyone can easily see it each day).  Divide $1,000,000 by 35 and see if the headmaster made a good decision about the calendar.  His arrogant and incompetent decision cost the school about $25,000 for each click he didn't want to make in 1999.  And now they do not know how to make the recurring cost go away.
I think i will have to settle defeated on this matter,  Will still award points.

What the company wanted was to have the database dates linked to Microsoft outlook application. for when the contracts are coming to an end. It was not a requirement of the project im doing they just said it would of been a nice extra.

ill award u the points
Helped me realise what i needed to do, A calendar in PHP with Events added to that calendar, Which then sends an email out every month with the users nearing their expiry period.
Thanks for the points - it's a good question, for sure. ~Ray