I have PHP times for the start and end times of an event. This is a simple <?php time(); ?>
for some future date's start and end time. I was wondering if anyone knew of a way to take either the numerical format (PHP time()) or taking some string value representing that time (I could do a strtotime($sometimevalue);
) and turn it into the required Google Calendar time format.
Enough talking - here is an example of the time format:
20150107T003000Z/20150107T023000Z
This equates to January 6th, 2015 at 5:30 PM to January 6th, 2015 at 7:30PM.
So can someone explain to me how to translate a time()
to this format?
Try this:
date_default_timezone_set('UTC');
$date = date("Ymd\THis\Z");
The first line sets the default timezone to use to be UTC (this is the "Z" at the end of the formatted time: Z = "Zulu Time"
). I did this since I don't know if Google is expecting a UTC time or not. If you can use other timezones, then you can use one of the other timezone formats available.
In the next line, I use date
to format the current Unix timestamp (when no timestamp is passed to date
it defaults to the current time - i.e. time()
). I'll break it apart for you:
Y
- The four-digit year m
- The two-digit (including leading zero, if necessary) month d
- The two-digit (including leading zero, if necessary) day of the month \T
- The literal character T
, which is a delimiter identifying that the time portion of the date is beginning. The slash is to escape the T, as it is otherwise used to display the timezone abbreviation (e.g. "PST") H
- The two-digit (including leading zero, if necessary) hour i
- The two-digit (including leading zero, if necessary) minute s
- The two-digit (including leading zero, if necessary) second \Z
- The literal character Z
, indicating zulu time as discussed above. The slash is to escape the T, as it is otherwise used to display the timezone in seconds from UTC. For reference, and to be sure I interpreted the question accurately, this code:
date_default_timezone_set('UTC');
echo date("Ymd\THis\Z", time());
Currently displays this result:
20110415T014623Z
I should note that you could also use gmdate()
in place of date()
and eliminate the need for the date_default_timezone_set()
call, since gmdate()
returns the result in GMT. I only hesitate to mention this because I've never been 100% clear on the difference, if any, between GMT and UTC, especially with other timezones/periods like BST (British Summer Time) and how they alter GMT, if at all. If someone could clarify this in the comments, I would be most appreciative.
Working solution taken from http://php.net/manual/en/function.date.php Credit goes to Boris Korobkov.
// boris at psyonline dot ru 14-Jun-2007 03:05
<?php
/**
* Get date in RFC3339
* For example used in XML/Atom
*
* @param integer $timestamp
* @return string date in RFC3339
* @author Boris Korobkov
*/
function date3339($timestamp=0) {
if (!$timestamp) {
$timestamp = time();
}
$date = date('Y-m-d\TH:i:s', $timestamp);
$matches = array();
if (preg_match('/^([\-+])(\d{2})(\d{2})$/', date('O', $timestamp), $matches)) {
$date .= $matches[1].$matches[2].':'.$matches[3];
} else {
$date .= 'Z';
}
return $date;
}
?>
Background: From http://www.ibm.com/developerworks/opensource/library/os-php-xpath/ I saw "The published and updated elements use the RFC 3339 time-stamp format. "
And figured I ought google "rfc3339 PHP" to find a function that implements this format
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With