I have an array containing time and price and time, I want to check which time array the price will correspond :
$myArray = array('8:00am'=>'200usd', '13:00pm'=>'300usd');
$timecurrent = date("H:ia");
I want the result: time 8:00am -> 12:59pm set price 200usd
Check my solution
$myArray = array('8:00am'=>'200usd', '9:00am'=>'250usd', '13:00pm'=>'300usd');
//Get current time
$timecurrent = DateTime::createFromFormat('H:ia', date('H:ia'));
$price = null;
$actualPrice = null;
foreach($myArray as $time=>$fee){
$date = DateTime::createFromFormat('H:ia', $time);
if($timecurrent<$date){ //if crossed current time
$actualPrice = $price; //Get previous element time and stop
break;
}
//Save fee for next iteration
$price = $fee;
}
echo $actualPrice; //250usd