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.



Your result can be seen below.

Result of php executing





Full code of Andrei.php

  1. <?php
  2.  
  3. function getArrivalTime(DateTime $dateTime, float $distanceInMeters)
  4. {
  5.     // Average speed 50 km/h or 50/3.6=~13.8 m/s
  6.     $speed = 13.8;
  7.  
  8.     // Coefficient depends on traffic jam
  9.     $speedCoefficientByHour = [
  10.         0 => 1,
  11.         1 => 1,
  12.         2 => 1,
  13.         3 => 1,
  14.         4 => 1,
  15.         5 => 1,
  16.         6 => 0.95,
  17.         7 => 0.8,
  18.         8 => 0.6,
  19.         9 => 0.65,
  20.         10 => 0.7,
  21.         11 => 0.75,
  22.         12 => 0.75,
  23.         13 => 0.65,
  24.         14 => 0.68,
  25.         15 => 0.7,
  26.         16 => 0.8,
  27.         17 => 0.7,
  28.         18 => 0.55,
  29.         19 => 0.65,
  30.         20 => 0.75,
  31.         21 => 0.9,
  32.         22 => 0.95,
  33.         23 => 1,
  34.     ];
  35.  
  36.     // step 1 second
  37.     $step = 1;
  38.  
  39.     $traveledDistance = 0;
  40.     while ($traveledDistance <= $distanceInMeters) {
  41.         $traveledDistance += ($speed * $speedCoefficientByHour[(int) date('G', $dateTime->getTimestamp())]) * $step;
  42.         $dateTime->add(new DateInterval('PT' . $step . 'S'));
  43.     }
  44.  
  45.     return $dateTime;
  46. }
  47.  
  48. $format = 'Y-m-d H:i:s';
  49.  
  50. // Case 1
  51. $startDateTime = DateTime::createFromFormat($format, '2020-01-02 15:16:17');
  52. $arrivalDateTime = getArrivalTime($startDateTime, 4200);
  53. echo 'case 1: ' . $arrivalDateTime->format($format) . "\n";
  54.  
  55. // Case 2
  56. $startDateTime = DateTime::createFromFormat($format, '2020-01-02 15:16:17');
  57. $arrivalDateTime = getArrivalTime($startDateTime, 15600);
  58. echo 'case 2: ' . $arrivalDateTime->format($format) . "\n";
  59.  
  60. // Case 3
  61. $startDateTime = DateTime::createFromFormat($format, '2020-01-02 15:16:17');
  62. $arrivalDateTime = getArrivalTime($startDateTime, 25367);
  63. echo 'case 3: ' . $arrivalDateTime->format($format) . "\n";
File Description
  • Andrei
  • PHP Code
  • 21 Jan-2020
  • 1.6 Kb
You can Share it: