neil_pearce

landingDateTime

Jan 25th, 2023
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.31 KB | Source Code | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. function landingDateTime(string $departureTimeZone, string $departureDateTime,
  5.                          string $flightTime, string $destinationTimeZone): array
  6. {
  7.     $departure = new DateTimeImmutable($departureDateTime, new DateTimeZone($departureTimeZone));
  8.     $dateTime['departure'] = $departure;
  9.  
  10.     $arrival = new DateTime($departureDateTime, new DateTimeZone($departureTimeZone));
  11.     [$hours, $minutes, $seconds] = explode(':', $flightTime);
  12.     $arrival->add(new DateInterval(sprintf('PT%sH%sM%sS', $hours, $minutes, $seconds)));
  13.     $arrival->setTimezone(new DateTimeZone($destinationTimeZone));
  14.     $dateTime['arrival'] = $arrival;
  15.  
  16.     return $dateTime;  // return a pair of datetime objects
  17. }
  18.  
  19.  
  20. $eol = empty($argc) ? '<br>' : PHP_EOL;  // determine end of line
  21.  
  22.  
  23. /*
  24.  * Set flight details
  25.  */
  26. $departureTimeZone = 'Europe/Paris';
  27. $departureDateTime = '2023-02-21 08:26:00';
  28. $flightTime = '10:15:07';
  29. $destinationTimeZone = 'America/Lima';
  30.  
  31. $landingDateTime = landingDateTime($departureTimeZone, $departureDateTime, $flightTime,
  32.                                    $destinationTimeZone);
  33.  
  34. printf('Depart: %s   Arrive: %s' . $eol . $eol,
  35.        $landingDateTime['departure']->format('d/M/Y H:i:s'),
  36.        $landingDateTime['arrival']->format('d/M/Y H:i:s'));
  37.  
Advertisement
Add Comment
Please, Sign In to add comment