Solved

Creating an ICS file from PHP

Posted on 2013-06-19
2
654 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 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