0

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.

swapfile
  • 415
  • 2
  • 19
  • At [so] you are expected to try to **write the code yourself**. After **[doing more research](//meta.stackoverflow.com/questions/261592)** if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a [**Minimal, Complete, and Verifiable example**](//stackoverflow.com/help/mcve). I suggest reading [ask] a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour] and read **[this](//meta.stackoverflow.com/questions/347937/)**. – John Conde Aug 25 '17 at 14:25
  • See also: [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/q/676824/250259) – John Conde Aug 25 '17 at 14:27

3 Answers3

1

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);
Farkie
  • 3,307
  • 2
  • 22
  • 33
1

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

William Perron
  • 1,288
  • 13
  • 18
0

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

Peter
  • 29,454
  • 5
  • 48
  • 60
  • Thanks, so far its clear. But if i loop through the array for example that way foreach ($a as & $v) { $diff=date_diff(strtotime($v),strtotime($v+1)); } How is it possible to get the value of the next array value? – swapfile Aug 25 '17 at 14:31