swapfile
swapfile

Reputation: 417

Calculate Timeslots between Timestamps in Array PHP

let's say i have an php array like that:

Array ( [0] => 2017-08-25 06:27:00 [1] => 2017-08-25 07:38:00 [2] => 2017-08-25 08:34:00 [3] => 2017-08-25 09:57:00 [4] => 2017-08-25 11:08:00 [5] => 2017-08-25 12:37:00 [6] => 2017-08-25 14:12:00 [7] => 2017-08-25 15:21:00 [8] => 2017-08-25 16:59:00 [9] => 2017-08-25 18:08:00 [10] => 2017-08-25 19:05:00 [11] => 2017-08-25 20:03:00 [12] => 2017-08-25 21:04:00 [13] => 2017-08-25 21:59:00 [14] => 2017-08-25 23:02:00 )

And want to calculate the time in minutes between each timestamp in the array.

Output should be Array ( [0] => 69 [1] => 56 [2] .... )

I have no Idea how to solve that for example with an foreach.

Upvotes: 0

Views: 293

Answers (3)

William Perron
William Perron

Reputation: 1398

Even though the answer has already been accepted, I thought I'd give my input nonetheless as I feel there are other options that are more robust and clear/easier to understand.

The Object-Oriented Approach

PHP has a collection of native objects specifically designed to handle date calculations, namely DateTime and DateInterval. making use of those objects will make the code easier to read and understand, which in turn means the code is easier to bug and more maintainable.

$dateArr = ['2017-08-25 06:27:00', '2017-08-25 07:38:00', '2017-08-25 08:34:00'];

$previousDate = '';
foreach($dateArr as $dateStr) {
    $curDate = new DateTime($dateStr);
    if(!empty($previousDate)) {
        $diff = $previousDate->diff($curDate);
        echo $diff->format('%i min').PHP_EOL;
    }

    $previousDate = $curDate;
}

This loop will output the following:

11 min
56 min

Of course, if you want to use this value for calculations, you'll need to do some extra manipulation to turn it into a numeric value type.

$min = $diff->format('%i');

if(is_numeric($min)) {
    $min = (int) $min;
    echo $min.' ('.gettype($min).')'.PHP_EOL;
}
else {
    echo 'Oops, something went wrong :('.PHP_EOL; // good place to throw an exception, this echo is only for demo purposes
}

Outputs:

11 (integer)
56 (integer)

Using the DateTime object will also allow you to catch badly formatted dates much more easily as it throws an exception instead of failing silently.

try {
    $wrongDate = new DateTime('foobar');
    echo $wrongDate->format('Y-m-d h:i:d').PHP_EOL;
}
catch(Exception $e) {
    echo $e->getMessage().PHP_EOL;  // the exception is caught
}

try {
    $wrongToTime = strtotime('foobar');
    echo $wrongToTome.PHP_EOL; // no exception si thrown and 
                               // an undefined variable notice is thrown
}
catch(Exception $e) {
    echo $e->getMessage().PHP_EOL;
}

Try it here!


Related Documentation

Upvotes: 1

Farkie
Farkie

Reputation: 3337

I've had a quick go, just looping through and checking if there's a next value, then subtracting it, and dividing by 60 to get minutes:

$dates = ['2017-08-25 06:27:00', '2017-08-25 07:38:00', '2017-08-25 08:34:00', '2017-08-25 09:57:00'];

$values = [];

foreach ($dates as $index => $date) {
    $date = strtotime($date);
    if (!empty($dates[$index+1])) {
        $nextDate = strtotime($dates[$index+1]);
        $values[] = ($nextDate-$date) / 60;
    }
}

var_dump($values);

Upvotes: 1

Peter
Peter

Reputation: 31701

Convert the strings into timestamps (seconds) using strtotime or strptime, then simply subtract.

Upvotes: 0

Related Questions