No Description

get-events.php 1.8KB

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