You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
957 B

  1. <?php
  2. $app->get('/api/timezone', function() use($app) {
  3. $params = $app->request()->params();
  4. if(k($params, 'latitude') !== null && k($params, 'longitude') !== null) {
  5. $lat = (float)$params['latitude'];
  6. $lng = (float)$params['longitude'];
  7. $tz = \p3k\Timezone::timezone_for_location($lat, $lng);
  8. $timezone = false;
  9. if($tz) {
  10. $timezone = new p3k\timezone\Result($tz);
  11. }
  12. if($timezone) {
  13. json_response($app, [
  14. 'timezone' => $timezone->name,
  15. 'offset' => $timezone->offset,
  16. 'seconds' => $timezone->seconds,
  17. 'localtime' => $timezone->localtime
  18. ]);
  19. } else {
  20. json_response($app, [
  21. 'error' => 'not_found',
  22. 'error_description' => 'No timezone was found for the requested location'
  23. ]);
  24. }
  25. } else {
  26. json_response($app, [
  27. 'error' => 'invalid_request',
  28. 'error_description' => 'Request was missing parameters'
  29. ], 400);
  30. }
  31. });