Skip to content
Advertisement

PHP Time() to Google Calendar Dates time format

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?

Advertisement

Answer

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-dTH: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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement