PowerRC's Blog

Posted by:
PowerRC

该同志还没添加个人介绍

4,473

[PHP] how to get holidays of USA with google calendar api

Before I post this blog , I did search around for this function.
I still prefer a simple one , that's why I would like to share with you.
Basically , I was using google calenda's api.
But I am using the public feed , so I don't need to register so called google api developer account.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public function getHolidayList() {
        $feedUrl = "http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/full?singleevents=true&futureevents=true&max-results=99&orderby=starttime&sortorder=a";
//about the parameters fo this feed , you could look it up at google calendar 's api document
//parameters I used , are to show 99 records , it should be enough for most cases


//we don't get PTO on all holidays :( , so , this array is to define holidays I need
        $presetHoliday = array(
            "Labor Day",
            "Christmas Eve",
            "Christmas Day",
            "New Year's Day",
            "Memorial Day",
            "Independence Day",
            "Thanksgiving Day"
        );
        $holidays = array();
        $calendar = simplexml_load_file($feedUrl);
        foreach ($calendar->entry as $item) {
            if (in_array(trim((string) $item->title), $presetHoliday)) { // remove this condition if you need to get all holidays
                $ns_gd = $item->children('http://schemas.google.com/g/2005');
                $gCalDate = date("Y-m-d", strtotime($ns_gd->when->attributes()->startTime));
                $holidays[$gCalDate]=(string) $item->title;
               
                //harcoded , because we need to define black friday is also holiday , but google doesn't think so
                if((string) $item->title=="Thanksgiving Day")
                {
                  $gCalDate = date("Y-m-d", strtotime($ns_gd->when->attributes()->startTime)+ 86400);
                  $holidays[$gCalDate]="Black Friday";  
                }
               
            }
        }
       //$holidays is the result
       
    }

版权申明

若文中未注明,则本文由 PowerRC 原创,转载请注明出处。

分享

相关文章

Leave a Reply

*

code

back up ↑