1

I'm trying to get a list of events on the current week from one public Google Calendar using the Google Api.

All my configurations are good, i can do any tests, list and show events in one web page, but i searched any sort of query for get the events of the current week using the server date with out success.

I think it's possible using the "q" option indicated in the Google Api documentation, but i don't know who i can indicate the current week.

EDIT: I found one solution and posted as answer.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
A. Cedano
  • 557
  • 7
  • 39

2 Answers2

4

Suppose I have 50 events on the week of 02-12-2017 to 02-18-2017. The first event starts at 5am on 02-12 and the last event starts at 8pm on 02-18. Using Central Standard Time (CST), this is how I did it:

  $service = new Google_Service_Calendar($client);

  $optParams = array(
    "timeMin" => "2017-02-12T05:00:00-06:00",
    "timeMax" => "2017-02-18T20:00:01-06:00"
  );

  $events = $service->events->listEvents('calendar@email.address', $optParams);

  foreach ($events->getItems() as $event) {    

    print $eventnum . " - Event Name: ". $event->summary . "<br>";

  }

I'm not sure if the q will help to achieve this. I tried but it only worked with names and description of events, not with start times or end times. I hope this information helps.

Morfinismo
  • 4,985
  • 4
  • 19
  • 36
  • Thanks. I talked about q option thinking that Google Calendar have the possibility of define one week by number. Calculating by php the current week, i thinked the possibility of one query as "q=weeknumber is 12" Reading in forums, it says that for show week numbers in Google calendars we need to use another calendar ! Then, my approach will be calculate by php the current week first and last date and pass these two values as timeMin, timeMax parameters. – A. Cedano Feb 14 '17 at 12:41
2

I'm posting here the solution i find for my question: I calculated the desired range of dates with strtotime, in my case, all days between previous sunday and next monday. Then i pass these parametres to my request:

function getEvents($service, $calendarId){
//De un Domingo al otro
$date_inicio = date('c',strtotime( "previous sunday" ));
$date_fin = date('c',strtotime( "next monday" ));
$optParams = array(
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => $date_inicio,
  'timeMax' => $date_fin
  );
  $results = $service->events->listEvents($calendarId, $optParams);

if (count($results->getItems()) == 0) {
    print "Ningún evento encontrado.\n";
} else {
    foreach ($results->getItems() as $event) {
        $start = $event->start->dateTime;
        if (empty($start)) {
            $start = $event->start->date;
        }
        printf("%s (%s)\n", $event->getSummary(), $start);
        echo $start.'<br>'.$event->getSummary().'<br>'.$event->getDescription().'<hr>';
        printf("%s (%s)\n<br><br>", $event->getDescription(), $start);
    }
}
}
A. Cedano
  • 557
  • 7
  • 39