3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array ( 0 => array ( 'datum_od' => '10.01.2020', 'datum_do' => '20.01.2020', 'abroad' ), 1 => array ( 'datum_od' => '10.02.2020', 'datum_do' => '20.02.2020', 'abroad'), 2 => array ( 'datum_od' => '10.03.2020', 'datum_do' => '20.03.2020', 'abroad'), 3 => array ( 'datum_od' => '10.04.2020', 'datum_do' => '20.04.2020', 'abroad')); $end_date = 0; if(date('d', strtotime($arr[0]['datum_od'])) != '01'){ $end_date = date('d.m.Y', strtotime($arr[0]['datum_od'] . ' -1 day')); $new[] = array('datum_od' => date('01.m.Y', strtotime($arr[0]['datum_od'])), 'datum_do' => $end_date, 'home'); } foreach($arr as $key => $item){ if(date("d.m.Y", strtotime($end_date . " +1 day")) != date('d.m.Y', strtotime($item['datum_od']))){ $end_date = date('d.m.Y', strtotime($item['datum_od'] . ' -1 day')); $new[] = array('datum_od' => date('d.m.Y', strtotime($arr[$key-1]['datum_do'] . " +1 day")), 'datum_do' => $end_date, 'home'); $new[] = $item; }else{ $new[] = $item; } // if(count($new) == 3) var_dump($end_date+1); } var_dump($new);
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.15, 8.1.0 - 8.1.2
array(8) { [0]=> array(3) { ["datum_od"]=> string(10) "01.01.2020" ["datum_do"]=> string(10) "09.01.2020" [0]=> string(4) "home" } [1]=> array(3) { ["datum_od"]=> string(10) "10.01.2020" ["datum_do"]=> string(10) "20.01.2020" [0]=> string(6) "abroad" } [2]=> array(3) { ["datum_od"]=> string(10) "21.01.2020" ["datum_do"]=> string(10) "09.02.2020" [0]=> string(4) "home" } [3]=> array(3) { ["datum_od"]=> string(10) "10.02.2020" ["datum_do"]=> string(10) "20.02.2020" [0]=> string(6) "abroad" } [4]=> array(3) { ["datum_od"]=> string(10) "21.02.2020" ["datum_do"]=> string(10) "09.03.2020" [0]=> string(4) "home" } [5]=> array(3) { ["datum_od"]=> string(10) "10.03.2020" ["datum_do"]=> string(10) "20.03.2020" [0]=> string(6) "abroad" } [6]=> array(3) { ["datum_od"]=> string(10) "21.03.2020" ["datum_do"]=> string(10) "09.04.2020" [0]=> string(4) "home" } [7]=> array(3) { ["datum_od"]=> string(10) "10.04.2020" ["datum_do"]=> string(10) "20.04.2020" [0]=> string(6) "abroad" } }
Output for 5.0.0 - 5.0.5
Notice: Undefined offset: -1 in /in/lCOGv on line 25 array(5) { [0]=> array(3) { ["datum_od"]=> string(10) "11.06.2021" ["datum_do"]=> string(10) "01.01.1970" [0]=> string(4) "home" } [1]=> array(3) { ["datum_od"]=> string(10) "10.01.2020" ["datum_do"]=> string(10) "20.01.2020" [0]=> string(6) "abroad" } [2]=> array(3) { ["datum_od"]=> string(10) "10.02.2020" ["datum_do"]=> string(10) "20.02.2020" [0]=> string(6) "abroad" } [3]=> array(3) { ["datum_od"]=> string(10) "10.03.2020" ["datum_do"]=> string(10) "20.03.2020" [0]=> string(6) "abroad" } [4]=> array(3) { ["datum_od"]=> string(10) "10.04.2020" ["datum_do"]=> string(10) "20.04.2020" [0]=> string(6) "abroad" } }
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9
array(4) { [0]=> array(3) { ["datum_od"]=> string(10) "10.01.2020" ["datum_do"]=> string(10) "20.01.2020" [0]=> string(6) "abroad" } [1]=> array(3) { ["datum_od"]=> string(10) "10.02.2020" ["datum_do"]=> string(10) "20.02.2020" [0]=> string(6) "abroad" } [2]=> array(3) { ["datum_od"]=> string(10) "10.03.2020" ["datum_do"]=> string(10) "20.03.2020" [0]=> string(6) "abroad" } [3]=> array(3) { ["datum_od"]=> string(10) "10.04.2020" ["datum_do"]=> string(10) "20.04.2020" [0]=> string(6) "abroad" } }

preferences:
82.93 ms | 465 KiB | 5 Q