3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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,29,31); $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' ))); var_dump(array_values(array_map(function($obj){ return $obj->format('d/m/Y'); },$datesRange))); /* // This way seems to work 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'); } // 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 - 7.1.0
array(92) { [0]=> string(10) "01/10/2012" [1]=> string(10) "02/10/2012" [2]=> string(10) "03/10/2012" [3]=> string(10) "04/10/2012" [4]=> string(10) "05/10/2012" [5]=> string(10) "06/10/2012" [6]=> string(10) "07/10/2012" [7]=> string(10) "08/10/2012" [8]=> string(10) "09/10/2012" [9]=> string(10) "10/10/2012" [10]=> string(10) "11/10/2012" [11]=> string(10) "12/10/2012" [12]=> string(10) "13/10/2012" [13]=> string(10) "14/10/2012" [14]=> string(10) "15/10/2012" [15]=> string(10) "16/10/2012" [16]=> string(10) "17/10/2012" [17]=> string(10) "18/10/2012" [18]=> string(10) "19/10/2012" [19]=> string(10) "20/10/2012" [20]=> string(10) "21/10/2012" [21]=> string(10) "22/10/2012" [22]=> string(10) "23/10/2012" [23]=> string(10) "24/10/2012" [24]=> string(10) "25/10/2012" [25]=> string(10) "26/10/2012" [26]=> string(10) "27/10/2012" [27]=> string(10) "28/10/2012" [28]=> string(10) "29/10/2012" [29]=> string(10) "30/10/2012" [30]=> string(10) "31/10/2012" [31]=> string(10) "01/11/2012" [32]=> string(10) "02/11/2012" [33]=> string(10) "03/11/2012" [34]=> string(10) "04/11/2012" [35]=> string(10) "05/11/2012" [36]=> string(10) "06/11/2012" [37]=> string(10) "07/11/2012" [38]=> string(10) "08/11/2012" [39]=> string(10) "09/11/2012" [40]=> string(10) "10/11/2012" [41]=> string(10) "11/11/2012" [42]=> string(10) "12/11/2012" [43]=> string(10) "13/11/2012" [44]=> string(10) "14/11/2012" [45]=> string(10) "15/11/2012" [46]=> string(10) "16/11/2012" [47]=> string(10) "17/11/2012" [48]=> string(10) "18/11/2012" [49]=> string(10) "19/11/2012" [50]=> string(10) "20/11/2012" [51]=> string(10) "21/11/2012" [52]=> string(10) "22/11/2012" [53]=> string(10) "23/11/2012" [54]=> string(10) "24/11/2012" [55]=> string(10) "25/11/2012" [56]=> string(10) "26/11/2012" [57]=> string(10) "27/11/2012" [58]=> string(10) "28/11/2012" [59]=> string(10) "29/11/2012" [60]=> string(10) "30/11/2012" [61]=> string(10) "01/12/2012" [62]=> string(10) "02/12/2012" [63]=> string(10) "03/12/2012" [64]=> string(10) "04/12/2012" [65]=> string(10) "05/12/2012" [66]=> string(10) "06/12/2012" [67]=> string(10) "07/12/2012" [68]=> string(10) "08/12/2012" [69]=> string(10) "09/12/2012" [70]=> string(10) "10/12/2012" [71]=> string(10) "11/12/2012" [72]=> string(10) "12/12/2012" [73]=> string(10) "13/12/2012" [74]=> string(10) "14/12/2012" [75]=> string(10) "15/12/2012" [76]=> string(10) "16/12/2012" [77]=> string(10) "17/12/2012" [78]=> string(10) "18/12/2012" [79]=> string(10) "19/12/2012" [80]=> string(10) "20/12/2012" [81]=> string(10) "21/12/2012" [82]=> string(10) "22/12/2012" [83]=> string(10) "23/12/2012" [84]=> string(10) "24/12/2012" [85]=> string(10) "25/12/2012" [86]=> string(10) "26/12/2012" [87]=> string(10) "27/12/2012" [88]=> string(10) "28/12/2012" [89]=> string(10) "29/12/2012" [90]=> string(10) "30/12/2012" [91]=> string(10) "31/12/2012" }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/fRvnS on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/fRvnS on line 18
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/fRvnS on line 18
Process exited with code 255.