3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php //$current_date = mktime(0, 0, 0, date("m"), date("d"), date("Y")); //$permit_active_date = mktime(0, 0, 0, date("m"), date("d"), date("Y")); $current_date = date_create("2018-10-25 00:00:00"); //mktime(0, 0, 0, 10, 25, 2018); $permit_active_date = date_create("2018-10-25 00:00:00"); $pay_frequency = "BW"; $UCDHS_MasterPaySchedule = array ( "BW" => array ( array ( "file_submit_date" => date_create("2018-09-14 00:00:00"), "pay_check_date" => date_create("2018-09-19 00:00:00"), "park_period_start" => date_create("2018-10-01 00:00:00"), "park_period_end" => date_create("2018-10-15 00:00:00"), "check_num" => 1 ), array ( "file_submit_date" => date_create("2018-09-28 00:00:00"), "pay_check_date" => date_create("2018-10-03 00:00:00"), "park_period_start" => date_create("2018-10-16 00:00:00"), "park_period_end" => date_create("2018-10-31 00:00:00"), "check_num" => 2 ), array ( "file_submit_date" => date_create("2018-10-12 00:00:00"), "pay_check_date" => date_create("2018-10-17 00:00:00"), "park_period_start" => date_create("2018-11-01 00:00:00"), "park_period_end" => date_create("2018-11-15 00:00:00"), "check_num" => 3 ), array ( "file_submit_date" => date_create("2018-11-08 00:00:00"), "pay_check_date" => date_create("2018-11-14 00:00:00"), "park_period_start" => date_create("2018-11-16 00:00:00"), "park_period_end" => date_create("2018-11-30 00:00:00"), "check_num" => 4 ), array ( "file_submit_date" => date_create("2018-11-21 00:00:00"), "pay_check_date" => date_create("2018-11-28 00:00:00"), "park_period_start" => date_create("2018-12-01 00:00:00"), "park_period_end" => date_create("2018-12-15 00:00:00"), "check_num" => 5 ), array ( "file_submit_date" => date_create("2018-12-07 00:00:00"), "pay_check_date" => date_create("2018-12-12 00:00:00"), "park_period_start" => date_create("2018-12-16 00:00:00"), "park_period_end" => date_create("2018-12-31 00:00:00"), "check_num" => 6 ) ) ); //reduce array to pay schedule // $arrPaySchedule = $UCDHS_MasterPaySchedule[$pay_frequency]; //find pay cycle where parking period includes permit start date $pay_cycle_for_active_date = get_pay_cycle_for_active_date($current_date, $permit_active_date, $UCDHS_MasterPaySchedule[$pay_frequency]); print $pay_cycle_for_active_date; /** * Number of days between two dates. * * @param date $dt1 First date * @param date $dt2 Second date * @return int */ function daysBetween($dt1, $dt2) { // return date_diff( // date_create($dt2), // date_create($dt1) // )->format('%a'); return date_diff( $dt2, $dt1 )->format('%a'); } function get_pay_cycle_for_active_date($current_date, $permit_active_date, $arr_pay_schedule) { // note: function assumes that pay period data is ordered earliest to latest, in the arrPaySchedule array //default values: $current_period_id = -1; $active_period_id = -1; //$return_file_submit_date //pay_check_date //park_period_start //park_period_end //check_num //loop through pay periods... foreach($arr_pay_schedule as $period_num => $period) { //find current parking period if ($current_date >= $period["park_period_start"] && $current_date <= $period["park_period_end"]) { $current_period_id = $period_num; } //find parking period which contains permit_active_date if ($permit_active_date >= $period["park_period_start"] && $permit_active_date <= $period["park_period_end"]) { $active_period_id = $period_num; } //determine number of days permit is valid in perid permit becomes valid $days_valid_in_active_period = daysBetween($permit_active_date, $period["park_period_end"]); } echo "current_period_id = ".$current_period_id."\f\r"; echo "active_period_id = ".$active_period_id."\f\r"; echo "days_valid_in_active_period = ".$days_valid_in_active_period."\f\r"; echo "permit_active_date = ".$permit_active_date."\f\r"; echo "period['park_period_end'] = ".$period["park_period_end"]."\f\r"; return ""; // $arrSelectedSchedule = array_filter($arrPaySchedule, function($e) use($permit_active_date) {return ($e["park_period_start"]<=$permit_active_date && $e["park_period_end"]>=$permit_active_date);}); // print_r($arrSelectedSchedule); // return array_search() // return count(array_filter($arrPaySchedule, function($e) use($permit_active_date) {return ($e["park_period_start"]<=$permit_active_date && $e["park_period_end"]>=$permit_active_date);})); } //$min=mktime(0,0,0,10,1,2018); //$max=mktime(0,0,0,10,4,2018); //print count(array_filter($UCDHSPaySchedule["BW"], function($e) use($min,$max) {return ($e["permit_active_date"]>$min && $e["permit_active_date"]<$max);})); //function gets the number of submittion dates between the $current_date and the submission date for permit starting on $active_date //function get_cycle_count($current_date, $active_date, $UCDHSPaySchedule) { //print count(array_filter($UCDHSPaySchedule["BW"], function($e) use($min,$max) {return ($e["permit_active_date"]>$min && //$e["permit_active_date"]<$max);})); //} //$cars = array ( //"Volvo" => array("Volvo",22,18), //"BMW" => array("BMW",15,13), //"Saab" => array("Saab",5,2), //"Land Rover" => array("Land Rover",17,15) //); //print $cars['Volvo'][0]; ?>
based on 8IWZO
Output for 7.1.0 - 7.3.0rc3
current_period_id = 1 active_period_id = 1 days_valid_in_active_period = 67 Recoverable fatal error: Object of class DateTime could not be converted to string in /in/VRmIu on line 115
Process exited with code 255.
Output for 5.6.38 - 7.0.28
current_period_id = 1 active_period_id = 1 days_valid_in_active_period = 67 Catchable fatal error: Object of class DateTime could not be converted to string in /in/VRmIu on line 115
Process exited with code 255.