Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate flight time

Tags:

timezone

I am working now on a function ot calculate the flight time. I have a very strange problem, because it is a new domain for me.

Here is one example:

Outward: 25.02.2011 11:45 Cologne - Las Vegas 26.02.2011 21:18

Return: 18.03.2011 09:49 Las Vegas - Cologne 19.03.2011 11:05

For this flight Kayak is showing Outward: 18:33 hours Return: 17:16 hours

Cologne is GMT +01:00 Las Vegas is America/Los_Angeles timezone -> -08:00

But in the example the flight durariion for the outward flight is calculated with -8 hours offset for Las Vegas and for the return flight with -7 hours offset for Las Vegas. Why?

I have see the timezones here: http://download.geonames.org/export/dump/timeZones.txt

Has anyone any idea?

Thanks Nik

like image 978
Nik Avatar asked May 22 '26 00:05

Nik


2 Answers

It's because Daylight Savings Time doesn't start on the same date everywhere.

Las Vegas: DST starts on Sunday, 13 March 2011
Cologne: DST starts on Sunday, 27 March 2011

So since your return flight is on the 18th of March, Las Vegas will be in DST, but Cologne won't be.

like image 170
Hans Olsson Avatar answered May 28 '26 07:05

Hans Olsson


Ok, I solve the problem. Thanks for the advice to convert both dates to UTC/GMT first.

Here is the trick:

$reset_timezone = date_default_timezone_get();

// convert the first time date_default_timezone_set($timezone_identifier1); $time1 = $date_from.' '.$time_from; $gmtime1 = gmdate('Y-m-d H:i', strtotime($time1));

// convert the second time date_default_timezone_set($timezone_identifier2); $time2 = $date_to.' '.$time_to; $gmtime2 = gmdate('Y-m-d H:i', strtotime($time2));

// reset the timezone again date_default_timezone_set($reset_timezone);

--> here calculate the difference < --

Hope it helps anyone else!

Cheers Nik

like image 20
Nik Avatar answered May 28 '26 07:05

Nik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!