3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $json = '{ "events": [{ "event_site_url": "https://www.yelp.com/events/london-the-european-information-security-summit-2017", "id": "london-the-european-information-security-summit-2017", "latitude": 51.5009690970451, "longitude": -0.119283199310303, "name": "The European Information Security Summit 2017", "time_start": "2018-02-21 17:00", "time_end": "2018-02-23 02:30" }] }'; $data = json_decode($json); $event = $data->events[0]; function get_nearest_timezone($cur_lat, $cur_long, $country_code = '') { $timezone_ids = ($country_code) ? DateTimeZone::listIdentifiers(DateTimeZone::PER_COUNTRY, $country_code) : DateTimeZone::listIdentifiers(); if($timezone_ids && is_array($timezone_ids) && isset($timezone_ids[0])) { $time_zone = ''; $tz_distance = 0; //only one identifier? if (count($timezone_ids) == 1) { $time_zone = $timezone_ids[0]; } else { foreach($timezone_ids as $timezone_id) { $timezone = new DateTimeZone($timezone_id); $location = $timezone->getLocation(); $tz_lat = $location['latitude']; $tz_long = $location['longitude']; $theta = $cur_long - $tz_long; $distance = (sin(deg2rad($cur_lat)) * sin(deg2rad($tz_lat))) + (cos(deg2rad($cur_lat)) * cos(deg2rad($tz_lat)) * cos(deg2rad($theta))); $distance = acos($distance); $distance = abs(rad2deg($distance)); // echo '<br />'.$timezone_id.' '.$distance; if (!$time_zone || $tz_distance > $distance) { $time_zone = $timezone_id; $tz_distance = $distance; } } } return $time_zone; } return 'unknown'; } function getClosestTimezone($lat, $lng) { $diffs = array(); foreach(DateTimeZone::listIdentifiers() as $timezoneID) { $timezone = new DateTimeZone($timezoneID); $location = $timezone->getLocation(); $tLat = $location['latitude']; $tLng = $location['longitude']; $diffLat = abs($lat - $tLat); $diffLng = abs($lng - $tLng); $diff = $diffLat + $diffLng; $diffs[$timezoneID] = $diff; } //asort($diffs); $timezone = array_keys($diffs, min($diffs)); return $timezone[0]; } //timezone from co-ordinate $tz = getClosestTimezone($event->latitude, $event->longitude); //From: Wednesday, Feb 21, 9:00 am $date = DateTime::createFromFormat('Y-m-d H:i', $event->time_start, new DateTimeZone($tz)); echo 'From: '.$date->format('l, M j, g:m a') . "\n"; //To: Thursday, Feb 22, 6:30 pm $date = DateTime::createFromFormat('Y-m-d H:i', $event->time_end, new DateTimeZone($tz)); echo 'From: '.$date->format('l, M j, g:m a') . "\n";
Output for 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.3.1
From: Wednesday, Feb 21, 5:02 pm From: Friday, Feb 23, 2:02 am