-1

My array as bellow :

$data = [
'0' => [
    'depart' => '2021-02-09 10:50:00',
    'arrival' => '2021-02-09 13:10:00',
],
'1' => [
    'depart' => '2021-02-09 14:50:00',
    'arrival' => '2021-02-09 15:30:00',
],
'2' => [
    'depart' => '2021-02-09 18:20:00',
    'arrival' => '2021-02-09 19:40:00',
]

];

From above array, I want to calculate different time to be :

0 = calculate different time between [0]['arrival'] and [1]['depart']
1 = calculate different time between [1]['arrival'] and [2]['depart']

My code :

$diff_time = [];
foreach ($data as $d => $dta) {
    $diff_time[$d] = timeDifference($dta['arrival'], $dta['depart']);
}

Thank you for assistance.

  • 1
    Does this answer your question? [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – El_Vanja Dec 19 '20 at 10:59
  • I think that is for between single start and end date only. How about with value as array above. Could you please advise when this question has been submitted. I can not find. Thank you – Adin Ramdhan Dec 19 '20 at 11:10
  • It's easy enough to adapt the code in the duplicate to process your array, for example as is shown in the answer below. – Nick Dec 19 '20 at 11:18

1 Answers1

1

you can do something below, then you can use any variable from date_diff like [y] => 0

[m] => 0 [d] => 0 [h] => 2 [i] => 50 [s] => 0

$data = [
'0' => [
    'depart' => '2021-02-09 10:50:00',
    'arrival' => '2021-02-09 13:10:00',
],
'1' => [
    'depart' => '2021-02-09 14:50:00',
    'arrival' => '2021-02-09 15:30:00',
],
'2' => [
    'depart' => '2021-02-09 18:20:00',
    'arrival' => '2021-02-09 19:40:00',
] ];


$diff_time = [];
foreach ($data as $d => $dta) {
    if(isset( $data[$d+1]) &&  $data[$d+1]){
        $dif = date_diff( date_create($data[$d]['arrival']), date_create($data[$d+1]['depart']));
        $diff_time[$d] = $dif;
    }
    
}
print_r($diff_time);

You will get output printed like:

Array

(
    [0] => DateInterval Object
        (
            [y] => 0
            [m] => 0
            [d] => 0
            [h] => 1
            [i] => 40
            [s] => 0
            [weekday] => 0
            [weekday_behavior] => 0
            [first_last_day_of] => 0
            [invert] => 0
            [days] => 0
            [special_type] => 0
            [special_amount] => 0
            [have_weekday_relative] => 0
            [have_special_relative] => 0
        )

    [1] => DateInterval Object
        (
            [y] => 0
            [m] => 0
            [d] => 0
            [h] => 2
            [i] => 50
            [s] => 0
            [weekday] => 0
            [weekday_behavior] => 0
            [first_last_day_of] => 0
            [invert] => 0
            [days] => 0
            [special_type] => 0
            [special_amount] => 0
            [have_weekday_relative] => 0
            [have_special_relative] => 0
        )

)
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37