I getting dates of product reservation to the temporary array with the quantity reserved for a given day.
Below you can see that I have several reservations set for the product with ID: 3139.
I am interested closest date from the present day, ex. in this case, 2021-12-29 until that day, I have 3 items reserved, and on 2040-06-13 the reservation is gone, so these 3 items are returned to stock.
I currently did a script that looks for the nearest reserved date:
function find_closest($array, $date) {
foreach($array as $day) {
$interval[] = abs(strtotime($date) > strtotime($day));
}
asort($interval);
$closest = key($interval);
echo $array[$closest];
}
I do not know exactly how to do it, so that he checks exactly that a given number of pieces is reserved until 2040-06-13, and then it goes to 0.
My Answer:
- strtotime($day['date']));_ }_ asort($interval);_ $closest_date = key($interval);_ return $array[$closest_date];_}_$reservations = array(_ array('date' => '2021-12-29', 'quantity' => 3),_ array('date' => '2040-06-13', 'quantity' => -3),_);_$closest_reservation = find_closest($reservations, date('Y-m-d'));_echo "Closest reservation date: " . $closest_reservation['date'] . " with quantity: " . $closest_reservation['quantity'];_?>_Output:_Closest reservation date: 2021-12-29 with quantity: 3
Rate this post
4 of 5 based on 3242 votesComments