PHP Time () on Google Calendar Time Dates

I have PHP time for the start and end time of an event. This is a simple <?php time(); ?> <?php time(); ?> for future start and end dates. I was wondering if anyone knows a way to take either a number format (PHP time ()) or take some string value representing this time (I could do strtotime($sometimevalue); ) and turn it into the required time format Google Calendar

Suffice it to say - here is an example of the time format:

 20150107T003000Z/20150107T023000Z 

This corresponds to January 6, 2015 at 17:30 to January 6, 2015 at 7:30 pm.

So can someone explain to me how to convert time() to this format?

+4
source share
3 answers

Working solution taken from http://php.net/manual/en/function.date.php The loan belongs 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('Ymd\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 "Published and updated items use the RFC 3339 timestamp format."

And I decided that google "rfc3339 PHP" should find a function that implements this format

+3
source

Try the following:

 date_default_timezone_set('UTC'); $date = date("Ymd\THis\Z"); 

The first line sets the default time zone for use in UTC (this is β€œZ” at the end of the formatted time: Z = "Zulu Time" ). I did this because I don’t know if Google is expecting UTC or not. If you can use other time zones, you can use one of the other available time zone formats .

In the next line, I use date to format the current Unix timestamp (when the timestamp is not passed to date , by default it corresponds to the current time - i.e. time() ). I will make it out for you:

  • Y - four-digit year
  • m - Two-digit (including initial zero, if necessary) month
  • d - two-digit (including the initial zero, if necessary) day of the month
  • \T - The literal character T , which is a separator that defines the beginning of the time part of the date. The slash is the exit from T, because otherwise it is used to display the reduction of the time zone (for example, "PST").
  • H - two-digit (including initial zero, if necessary) hour
  • i - two-digit (including leading zero, if necessary) minute
  • s - two-digit (including leading zero, if necessary) second
  • \Z - The literal character Z indicating zulu time, as discussed above. A slash is the exit from T, because otherwise it is used to display the time zone in seconds from UTC.

For reference, and of course, I correctly interpreted the question, this code:

 date_default_timezone_set('UTC'); echo date("Ymd\THis\Z", time()); 

Shows this result:

 20110415T014623Z 

It should be noted that instead of date() you can use gmdate() and eliminate the need to call date_default_timezone_set() , since gmdate() returns the result in GMT. I am only too shy to mention this because I have never been 100% understandable the difference, if any, between GMT and UTC, especially with other time zones / periods such as BST (British Daylight Saving Time) and how they change GMT if at all. If someone could clarify this in the comments, I would be very grateful.

+5
source

It's a bit outdated, but Google expects to get the time in ISO 8601. Example:

 $objDateTime = new DateTime('NOW'); $isoDate = $objDateTime->format(DateTime::ISO8601); 

A SOURCE

+1
source

Source: https://habr.com/ru/post/1348199/


All Articles