Calculate hours between dates in different time zo

2019-09-12 17:54发布

问题:

I have an array of dates (in different time zones GMT) and I would like to calculate the hours that have elapsed between the first and the last date.

For example, this would be an array of dates:

[
    1 => 2016-06-05T08:45:00.000+02:00,
    2 => 2016-06-05T09:55:00.000+02:00,
    3 => 2016-06-05T12:10:00.000+02:00,
    4 => 2016-06-05T14:25:00.000-04:00
]

I want to calculate the hours that have elapsed since the date with index 1 to 2, 2 to 3 and from 3 to 4. The problem is I do not know how to calculate according to the time zone, if I must add or subtract hours to get the right result.

I need to know how it is calculated to develop the code. It would be something (for lack of calculated according to the time zone):

$tIda = 0;

foreach ($times0 as $i => $time)
{
    if ($i < sizeof($times0) - 1)
    {
        $datetime1 = strtotime(substr($time, 0, 10) . ' ' . substr($time, 11, 8));
        $datetime2 = strtotime(substr($times0[$i + 1], 0, 10) . ' ' . substr($times0[$i + 1], 11, 8));
        $interval  = abs($datetime2 - $datetime1);
        $tIda += round($interval / 60);
    }
}

Thanks.

回答1:

The best solution would be to use DateTime classes.

First, you create \DateTime objects from date strings:

$datetime1 = new \DateTime($time);
$datetime2 = new \DateTime($times0[$i + 1]);

Then you can calculate the difference between two dates using diff() method:

$diff = $datetime1->diff($datetime2);

$diff is an instance of \DateTimeInterval, and you can format this difference any way you like, for example:

$diff->format('%H:%I:%S')


回答2:

strtotime() parses time zones just fine:

$a = '2016-06-05T12:10:00.000+02:00';
$b = '2016-06-05T14:25:00.000-04:00';

$ts_a = strtotime($a);
$ts_b = strtotime($b);
var_dump($a, date('r', $ts_a), $b, date('r', $ts_b));
string(29) "2016-06-05T12:10:00.000+02:00"
string(31) "Sun, 05 Jun 2016 12:10:00 +0200"
string(29) "2016-06-05T14:25:00.000-04:00"
string(31) "Sun, 05 Jun 2016 20:25:00 +0200"

... and once you have a Unix timestamp you can forget about time zones because a Unix timestamp is a fixed moment in time (not a relative local time).

Since you've added tags for the DateTime class:

$a = '2016-06-05T12:10:00.000+02:00';
$b = '2016-06-05T14:25:00.000-04:00';

$dt_a = new DateTime($a);
$dt_b = new DateTime($b);
var_dump($dt_a->diff($dt_b)->format('%h')); // Prints: 8


回答3:

You should use the DateTime and DateInterval classes. They will handle all the timezome hazzle for you.

foreach ($times as $i => $time) {
    if ($i > 0) { // not on the 1st element
        $datetime1 = new DateTime($times[$i-1]);
        $datetime2 =  new DateTime($times[$i]);
        $interval = $datetime1->diff($datetime2);
        $interval_hours = ($interval->invert ? -1 : 1) * ($interval->days *24 + $interval->h);
        // days are the total days, h are the hours in day
        // invert is negative, if $datetime2 is before $datetime1
        echo "Diff from ".$times[$i-1]." to ".$times[$i].": ".$interval_hours."\n";
    }
}