3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Set demo vars $year = 2012; $months = array(10,11,12); $days = array(1,2,3,4,6,7,8,9,11,12,13,14,16,17,18,19,21,22,23,24,26,27,28,30); $dateWs = array(); foreach($months AS $month) foreach($days AS $day) $datesWS[] = new DateTime(implode('-',array($year,$month,$day))); // Push DateTime object to the WebService array // Shuffle shuffle($datesWS); // Sort dates usort($datesWS, function ($a, $b){ return ($a == $b) ? 0 : (($a > $b) ? 1 : -1); }); $dateIni = count($datesWS)>0 ? reset($datesWS) : 0; // Get first date $dateEnd = count($datesWS)>0 ? end($datesWS) : 0; // Get last date $interval = new DateInterval('P1D'); // Set date interval as 1 day // Generate dates range and Transform iterator to array $datesRange = iterator_to_array(new DatePeriod($dateIni, $interval, $dateEnd->modify( '+1 day' ))); // This way seems to work $excludeDates = array(); foreach($datesRange AS $date) { if(!in_array($date->format('d/m/Y'),array_values(array_map(function($obj){ return $obj->format('d/m/Y'); },$datesWS)))) $excludeDates[] = $date->format('d/m/Y'); } var_dump($excludeDates); /* // This way should be more efficient, but I'm fed up with it! // Get excluded dates $excludeDates = array_udiff($datesRange, $datesWS, function ($a, $b) { return $a == $b ? 0 : 1; }); // Comment/Uncomment this in order to print this method or the other one // Print array(objects) as array(strings) var_dump(array_values(array_map(function($obj){ return $obj->format('d/m/Y'); },$datesWS))); var_dump(array_values(array_map(function($obj){ return $obj->format('d/m/Y'); },$datesRange))); var_dump(array_values(array_map(function($obj){ return $obj->format('d/m/Y'); },$excludeDates))); */ ?>
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7
array(20) { [0]=> string(10) "05/10/2012" [1]=> string(10) "10/10/2012" [2]=> string(10) "15/10/2012" [3]=> string(10) "20/10/2012" [4]=> string(10) "25/10/2012" [5]=> string(10) "29/10/2012" [6]=> string(10) "31/10/2012" [7]=> string(10) "05/11/2012" [8]=> string(10) "10/11/2012" [9]=> string(10) "15/11/2012" [10]=> string(10) "20/11/2012" [11]=> string(10) "25/11/2012" [12]=> string(10) "29/11/2012" [13]=> string(10) "05/12/2012" [14]=> string(10) "10/12/2012" [15]=> string(10) "15/12/2012" [16]=> string(10) "20/12/2012" [17]=> string(10) "25/12/2012" [18]=> string(10) "29/12/2012" [19]=> string(10) "30/12/2012" }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/MouNr on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/MouNr on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/MouNr on line 18
Process exited with code 255.

preferences:
181.65 ms | 401 KiB | 207 Q