Reputation: 3773
I have a date that I am storing as a timestamp through the formatDate function in jQuery. I am then retriving this value to make an ics file, a calender file that adds the event time and details to the users calender. However the timestamp format isn't working in the ics file, the correct date isn't being added, so I need to convert it to a value that looks like 20091109T101015Z
. It's current format as a timestamp looks like 1344466800000
This is from this example which is what I am following to create my ics file.
My link to the php file is http:// domain. com/icsCreator.php?startDate=1344380400000&endDate=1345503600000&event=Space%20Weather%20Workshop&location=London
Current my ics file looks like
<?php
$dtStart=$_GET['startDate'];
$dtEnd=$_GET['endDate'];
$eventName=$_GET['event'];
$location=$_GET['location'];
...
echo "CREATED:20091109T101015Z\n";
echo "DESCRIPTION:$eventName\n";
echo "DTEND:$dtEnd\n";
echo "DTSTART:".$dtStart."\n";
echo "LAST-MODIFIED:20091109T101015Z\n";
echo "LOCATION:$location\n";
...
?>
Upvotes: 3
Views: 12412
Reputation: 86
The \Z is telling the system the timezone. Z being Zulu or Universal time.
If you leave that out - then you are presuming that the timezone settings on the end users calendar application, match those of your system generating the timestamps.
In the USA alone there are multiple timezones - so you can't make that assumption just based on your users being in the same country as you.
In order for dates to go into the calendar correctly, you need to specify the timezone offset from UTC as plus or minus Hours and Minutes
NOTE: date('Ymd\THisP') ; // P is an offset against GMT and should work for all calendar purposes.
That would out put somthing like this for a 1hr shift from GMT
20150601T10:38+01:00
When working with Dates in PHP it's best to use the DateTime object, then you can easily work with and change the timezones.
// Start with your local timezone e.g
$timezone = new \DateTimeZone('Europe/Amsterdam') ;
// Don't be tempted to use a timezone abbreviation like EST
// That could mean Eastern Standard Time for USA or Australia.
// Use a full timezone: http://php.net/manual/en/timezones.php
$eventdate = new DateTime ( '1st September 2015 10:30', $timezone);
// Convert the time to Universal Time
$eventdate->setTimezone( new DateTimeZone('UTC') ) ; // Universal / Zulu time
// Return Event Date/Time in calendar ICS friendly format
// comfortable in the knowledge that it is really in UTC time
return $eventdate->format('Ymd\THis\Z') ;
Upvotes: 6
Reputation: 7302
See if this works:
date('Ymd\THis', $time)
Here $time
could be startDate
or endDate
from your query string. If you don't want the time:
date('Ymd', $time)
NOTE (Thanks to Nicola) Here, $time
must be a valid UNIX timestamp i.e. it must represent the number of seconds since the epoch. If it represents the number of milliseconds, you need to first divide it by 1000.
EDIT As pointed out by lars k, you need to add \Z
to the end of both the strings.
EDIT As pointed out by Nicola, you don't really need it.
Upvotes: 6