Solved

Creating an ICS file from PHP

Posted on 2013-06-19
2
660 Views
Last Modified: 2013-06-19
I am trying to create an ICS file from a php script, and everything works fine, except for my times.

I'm not sure where to go or what to look for but all the times are wrong. The dates of the event are correct, but my start and end time will be a few hours before the expected event.

The start & end dates for this particular one is 6/19/2013 8am until 6/19/2013 at 9:30am

The times given in the ICS file are from 3:00am until 4:30am

$fullStart = $firstDay . ' ' . $startTime;
	$fullEnd = $returnDay . ' ' . $endTime;
		
		
	$startDate = date("M j, Y H:i", strtotime($fullStart));
	$endDate = date("M j, Y H:i", strtotime($fullEnd));
	
	$newStartTime = explode(":", $startTime);
	$Start1 = $newStartTime[0];
	$Start2 = $newStartTime[1];
	
	$startTime = $Start1.$Start2;
	
	$newEndTime = explode(":", $endTime);
	$End1 = $newEndTime[0];
	$End2 = $newEndTime[1];
	
	$endTime = $End1.$End2;
	
	$title = $userNotes;
	$description = "";
	$location = $resourceNeeded;
	$start_date = $startDate;
	$end_date = $endDate;
	$start_time = $_POST['startTime'];
	$end_time = $_POST['endTime'];
	
	$file_name = microtime(true) .'.vcs';

	$description = str_replace(array("\r\n", "\n"), array('=0D=0A', '=0D=0A'), $description);
	$str_start_date = strtotime($start_date);
	$str_end_date = strtotime($end_date);

	$start_time = date("Ymd", $str_start_date) .'T'. date("H", $str_start_date) . date("i", $str_start_date) .'00Z';

	if($str_end_date !== false){
	$end_time = date("Ymd", $str_end_date) .'T'.date("H", $str_end_date) . date("i", $str_end_date).'00Z';
	}

	$text_vcs = '';
	$text_vcs .= "BEGIN:VCALENDAR"."\n";
	$text_vcs .= "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN"."\n";
	$text_vcs .= "VERSION:1.0"."\n";
	$text_vcs .= "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN"."\n";
	$text_vcs .= "VERSION:1.0"."\n";
	$text_vcs .= "BEGIN:VEVENT"."\n";

	$text_vcs .= "DTSTART:".$start_time."\n";

	if($end_time) {
	$text_vcs .= "DTEND:". $end_time ."\n";
	}

	$text_vcs .= "LOCATION;ENCODING=QUOTED-PRINTABLE:". $location ."\n";
	$text_vcs .= "UID:040000008200E00074C5B7101A82E00800000000109E60CBFACAC7010000000000000000100"."\n";
	$text_vcs .= " 000004844976B382CE44C937F56D327FEE354"."\n";
	$text_vcs .= "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:". $description ."=0D=0A"."\n";
	$text_vcs .= "SUMMARY;ENCODING=QUOTED-PRINTABLE:". $title ."\n";
	$text_vcs .= "END:VEVENT"."\n";
	$text_vcs .= "END:VCALENDAR"."\n";

	ini_set('zlib.output_compression','Off');

	header('Pragma: public');
	header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
	header('Last-Modified: '. gmdate('D, d M Y H:i:s') . ' GMT');
	header('Cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: pre-check=0, post-check=0, max-age=0');
	header("Pragma: no-cache");
	header("Expires: 0");
	header('Content-Transfer-Encoding: none');
	header('Content-Type: application/force-download;');
	header('Content-Disposition: attachment; filename="'. $file_name .'"');

	echo $text_vcs;
		}
	}

Open in new window

0
Comment
Question by:t3chguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 7

Accepted Solution

by:
Robert Saylor earned 500 total points
ID: 39260724
I used this and it works on Outlook and Apple.

usage:

                $result = sendIcalEmail($firstname,$lastname,$email,$meeting_date,$meeting_name,$meeting_duration,$meeting_text);

Credits: I forgot where I downloaded this code from...

//$firstname is the first name of target
//$lastname is the last name of target
//$email is the targets email address
//$meeting_date is straight from a DATETIME mysql field and assumes UTC.
//$meeting_name is the name of your meeting
//$meeting_duretion is the duration of your meeting in seconds (3600 = 1 hour)

FUNCTION sendIcalEmail($firstname,$lastname,$email,$meeting_date,$meeting_name,$meeting_duration,$meeting_text) {

        $from_name = "Your company Here";
        $from_address = "info@yourname.com";
        $subject = "$meeting_name"; //Doubles as email subject and meeting subject in calendar
        $meeting_description = "$meeting_text\n\n";
        $meeting_location = "Your location here"; //Where will your meeting take place


        //Convert MYSQL datetime and construct iCal start, end and issue dates
        $meetingstamp = STRTOTIME($meeting_date . " EDT");
        $dtstart= GMDATE("Ymd\THis\Z",$meetingstamp);
        $dtend= GMDATE("Ymd\THis\Z",$meetingstamp+$meeting_duration);
        $todaystamp = GMDATE("Ymd\THis\Z");

        //Create unique identifier
        $cal_uid = DATE('Ymd').'T'.DATE('His')."-".RAND()."@yournamehere.com";

        //Create Mime Boundry
        $mime_boundary = "----Meeting Booking----".MD5(TIME());

        //Create Email Headers
        $headers = "From: ".$from_name." <".$from_address.">\n";
        $headers .= "Reply-To: ".$from_name." <".$from_address.">\n";

        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
        $headers .= "Content-class: urn:content-classes:calendarmessage\n";

        //Create Email Body (HTML)
        $message .= "--$mime_boundary\n";
        $message .= "Content-Type: text/html; charset=UTF-8\n";
        $message .= "Content-Transfer-Encoding: 8bit\n\n";

        $message .= "<html>\n";
        $message .= "<body>\n";
        //$message .= "$meeting_date\n";
        $message .= '<p>'.$meeting_text.'</p>';
        $message .= "</body>\n";
        $message .= "</html>\n";
        $message .= "--$mime_boundary\n";

        //Create ICAL Content (Google rfc 2445 for details and examples of usage)
        $ical =    'BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
VERSION:2.0
METHOD:PUBLISH
BEGIN:VEVENT
ORGANIZER:MAILTO:'.$from_address.'
DTSTART:'.$dtstart.'
DTEND:'.$dtend.'
LOCATION:'.$meeting_location.'
TRANSP:OPAQUE
SEQUENCE:0
UID:'.$cal_uid.'
DTSTAMP:'.$todaystamp.'
DESCRIPTION:'.$meeting_description.'
SUMMARY:'.$subject.'
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR';

        $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
        $message .= "Content-Transfer-Encoding: 8bit\n\n";
        $message .= $ical;

        //SEND MAIL
        $mail_sent = @MAIL( $email, $subject, $message, $headers );

        IF($mail_sent)     {
                RETURN TRUE;
        } ELSE {
                RETURN FALSE;
        }

}

Open in new window

0
 
LVL 1

Author Closing Comment

by:t3chguy
ID: 39260822
Simple, easy to follow script.  Thanks so much!
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question