Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 697
  • Last Modified:

Creating an ICS file from PHP

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
t3chguy
Asked:
t3chguy
1 Solution
 
Robert SaylorSenior DeveloperCommented:
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
 
t3chguyAuthor Commented:
Simple, easy to follow script.  Thanks so much!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now