Using PHP to parse Google’s XML calendar. End date is disabled for one day.

I am trying to create a webpage and the next three events on the Google calendar will appear on the next page. I use this PHP (http://james.cridland.net/code/google-calendar.html) to access my XML feed and format it in HTML.

The problem I am facing is that for some reason a new day starts at 11 in the morning. For example, if there is an event in the Google calendar from 10:00 on December 20 that lasts an hour, my PHP output will show an event that starts at 10:00 on the 20th, which ends at 11:00 on the 21st. Otherwise, it works fine.

I set the time to local (New Zealand) time on my Google Calendar account and on PHP using date_default_timezone_set("Pacific/Auckland");

A terrible string that calculates the end date,

 $gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+date("Z",strtotime($ns_gd->when->attributes()->endTime))); 

where $ dateformat is a string with a date format.

Google XML Calendar gives start and end times

 2011-12-22T10:00:00.000+13:00 2011-12-23T11:00:00.000+13:00 

respectively, and PHP calculates the time frame from 10:00 a.m. December 22, 2011 to 14:00 on December 23, 2011.

What's happening?!?!

+4
source share
2 answers

This line is really terrible:

 $gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)+ date("Z",strtotime($ns_gd->when->attributes()->endTime))); 

strtotime can handle this type of ISO 8601 date just fine. This piece of code is probably written under the assumption that strtotime rejects the time zone and returns the date-time in UTC, and therefore the correction of the “correction” of the time zone must be calculated manually - which means +date("Z", ...) (with "Z" the second parameter - timestamp - is actually ignored).

So, in your example, 13 hours are added to your dates. And 10:00 + 13:00 = 23:00 (11 pm), which is still on the same day, but 11:00 + 13:00 = 24:00 (12 am), which is actually 00:00 on a new day.

So the correct way to convert the date is:

 $gCalDateEnd = date($dateformat, strtotime($ns_gd->when->attributes()->endTime)); 
+1
source

Try the zend framework for the Google calendar (it worked for me better than reinventing the wheel): http://framework.zend.com/manual/en/zend.gdata.calendar.html (look at the examples, they are quite easy and useful)

+1
source

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


All Articles