3v4l.org

run code in 300+ PHP versions simultaneously
<?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 = ".date_format($permit_active_date,'Y-m-d')."\f\r"; echo "period['park_period_end'] = ".date_format($period["park_period_end"],'Y-m-d')."\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]; ?>
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
current_period_id = 1 active_period_id = 1 days_valid_in_active_period = 67 permit_active_date = 2018-10-25 period['park_period_end'] = 2018-12-31
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 current_period_id = 1 active_period_id = 1 days_valid_in_active_period = 67 permit_active_date = 2018-10-25 period['park_period_end'] = 2018-12-31

preferences:
207.07 ms | 402 KiB | 287 Q