Debian Codegraph @

tests/manual/php/get-events.php

raw
1 <?php
2
3 //--------------------------------------------------------------------------------------------------
4 // This script reads event data from a JSON file and outputs those events which are within the range
5 // supplied by the "start" and "end" GET parameters.
6 //
7 // An optional "timeZone" GET parameter will force all ISO8601 date stings to a given timeZone.
8 //
9 // Requires PHP 5.2.0 or higher.
10 //--------------------------------------------------------------------------------------------------
11
12 // Require our Event class and datetime utilities
13 require dirname(__FILE__) . '/utils.php';
14
15 // Short-circuit if the client did not give us a date range.
16 if (!isset($_GET['start']) || !isset($_GET['end'])) {
17 die("Please provide a date range.");
18 }
19
20 // Parse the start/end parameters.
21 // These are assumed to be ISO8601 strings with no time nor timeZone, like "2013-12-29".
22 // Since no timeZone will be present, they will parsed as UTC.
23 $range_start = parseDateTime($_GET['start']);
24 $range_end = parseDateTime($_GET['end']);
25
26 // Parse the timeZone parameter if it is present.
27 $time_zone = null;
28 if (isset($_GET['timeZone'])) {
29 $time_zone = new DateTimeZone($_GET['timeZone']);
30 }
31
32 // Read and parse our events JSON file into an array of event data arrays.
33 $json = file_get_contents(dirname(__FILE__) . '/../json/events.json');
34 $input_arrays = json_decode($json, true);
35
36 // Accumulate an output array of event data arrays.
37 $output_arrays = array();
38 foreach ($input_arrays as $array) {
39
40 // Convert the input array into a useful Event object
41 $event = new Event($array, $time_zone);
42
43 // If the event is in-bounds, add it to the output
44 if ($event->isWithinDayRange($range_start, $range_end)) {
45 $output_arrays[] = $event->toArray();
46 }
47 }
48
49 // Send JSON to the client.
50 echo json_encode($output_arrays);