Andrei - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Andrei.php
- <?php
- function getArrivalTime(DateTime $dateTime, float $distanceInMeters)
- {
- // Average speed 50 km/h or 50/3.6=~13.8 m/s
- $speed = 13.8;
- // Coefficient depends on traffic jam
- $speedCoefficientByHour = [
- 0 => 1,
- 1 => 1,
- 2 => 1,
- 3 => 1,
- 4 => 1,
- 5 => 1,
- 6 => 0.95,
- 7 => 0.8,
- 8 => 0.6,
- 9 => 0.65,
- 10 => 0.7,
- 11 => 0.75,
- 12 => 0.75,
- 13 => 0.65,
- 14 => 0.68,
- 15 => 0.7,
- 16 => 0.8,
- 17 => 0.7,
- 18 => 0.55,
- 19 => 0.65,
- 20 => 0.75,
- 21 => 0.9,
- 22 => 0.95,
- 23 => 1,
- ];
- // step 1 second
- $step = 1;
- $traveledDistance = 0;
- while ($traveledDistance <= $distanceInMeters) {
- $traveledDistance += ($speed * $speedCoefficientByHour[(int) date('G', $dateTime->getTimestamp())]) * $step;
- $dateTime->add(new DateInterval('PT' . $step . 'S'));
- }
- return $dateTime;
- }
- $format = 'Y-m-d H:i:s';
- // Case 1
- $startDateTime = DateTime::createFromFormat($format, '2020-01-02 15:16:17');
- $arrivalDateTime = getArrivalTime($startDateTime, 4200);
- echo 'case 1: ' . $arrivalDateTime->format($format) . "\n";
- // Case 2
- $startDateTime = DateTime::createFromFormat($format, '2020-01-02 15:16:17');
- $arrivalDateTime = getArrivalTime($startDateTime, 15600);
- echo 'case 2: ' . $arrivalDateTime->format($format) . "\n";
- // Case 3
- $startDateTime = DateTime::createFromFormat($format, '2020-01-02 15:16:17');
- $arrivalDateTime = getArrivalTime($startDateTime, 25367);
- echo 'case 3: ' . $arrivalDateTime->format($format) . "\n";
File Description
- Andrei
- PHP Code
- 21 Jan-2020
- 1.6 Kb
You can Share it:
Latest PHP Pastes