Andy2024 Posted 11 hours ago Share Posted 11 hours ago I am trying to get values from an array with a date range This is the var_dump assuming from date is 06 May 2025 and to date is 22 May 2025 [0]=> string(10) "2025-05-06" [1]=> string(10) "2025-05-07" [2]=> string(10) "2025-05-08" [3]=> string(10) "2025-05-09" [4]=> string(10) "2025-05-10" [5]=> string(10) "2025-05-11" [6]=> string(10) "2025-05-12" [7]=> string(10) "2025-05-13" [8]=> string(10) "2025-05-14" [9]=> string(10) "2025-05-15" [10]=> string(10) "2025-05-16" [11]=> string(10) "2025-05-17" [12]=> string(10) "2025-05-18" [13]=> string(10) "2025-05-19" [14]=> string(10) "2025-05-20" [15]=> string(10) "2025-05-21" [16]=> string(10) "2025-05-22" It then searches the following array $count = 0; foreach($seasonPrices as $row) { $price[$count]['name'] = $row->SeasonName; $price[$count]['days'] = implode(',', Cal::bookedArray($row->SeasonStart, $row->SeasonEnd)); $price[$count]['price'] = $row->SeasonPrice; $count++; } unset($count); [2]=> array(3) { ["name"]=> string(16) "Spring half-term" ["days"]=> string(120) "2025-02-21,2025-02-22,2025-02-23,2025-02-24,2025-02-25,2025-02-26,2025-02-27,2025-02-28,2025-03-01,2025-03-02,2025-03-03" ["price"]=> string(3) "100" } [3]=> array(3) { ["name"]=> string(6) "Easter" ["days"]=> string(186) "2025-04-11,2025-04-12,2025-04-13,2025-04-14,2025-04-15,2025-04-16,2025-04-17,2025-04-18,2025-04-19,2025-04-20,2025-04-21,2025-04-22,2025-04-23,2025-04-24,2025-04-25,2025-04-26,2025-04-27" ["price"]=> string(3) "100" } [4]=> array(3) { ["name"]=> string(7) "May Day" ["days"]=> string(43) "2025-05-02,2025-05-03,2025-05-04,2025-05-05" ["price"]=> string(3) "110" } [5]=> array(3) { ["name"]=> string(16) "Summer half-term" ["days"]=> string(109) "2025-05-23,2025-05-24,2025-05-25,2025-05-26,2025-05-27,2025-05-28,2025-05-29,2025-05-30,2025-05-31,2025-06-01" ["price"]=> string(3) "110" } [6]=> array(3) { ["name"]=> string(6) "Summer" ["days"]=> string(505) "2025-07-18,2025-07-19,2025-07-20,2025-07-21,2025-07-22,2025-07-23,2025-07-24,2025-07-25,2025-07-26,2025-07-27,2025-07-28,2025-07-29,2025-07-30,2025-07-31,2025-08-01,2025-08-02,2025-08-03,2025-08-04,2025-08-05,2025-08-06,2025-08-07,2025-08-08,2025-08-09,2025-08-10,2025-08-11,2025-08-12,2025-08-13,2025-08-14,2025-08-15,2025-08-16,2025-08-17,2025-08-18,2025-08-19,2025-08-20,2025-08-21,2025-08-22,2025-08-23,2025-08-24,2025-08-25,2025-08-26,2025-08-27,2025-08-28,2025-08-29,2025-08-30,2025-08-31,2025-09-01" ["price"]=> string(3) "120" } [7]=> array(3) { ["name"]=> string(16) "Autumn half-term" ["days"]=> string(120) "2025-10-24,2025-10-25,2025-10-26,2025-10-26,2025-10-27,2025-10-28,2025-10-29,2025-10-30,2025-10-31,2025-11-01,2025-11-02" ["price"]=> string(3) "110" } [8]=> array(3) { ["name"]=> string(10) "Christmas " ["days"]=> string(186) "2025-12-19,2025-12-20,2025-12-21,2025-12-22,2025-12-23,2025-12-24,2025-12-25,2025-12-26,2025-12-27,2025-12-28,2025-12-29,2025-12-30,2025-12-31,2026-01-01,2026-01-02,2026-01-03,2026-01-04" ["price"]=> string(3) "120" } [9]=> array(3) { ["name"]=> string(16) "Spring half-term" ["days"]=> string(109) "2026-02-20,2026-02-21,2026-02-22,2026-02-23,2026-02-24,2026-02-25,2026-02-26,2026-02-27,2026-02-28,2026-03-01" ["price"]=> string(3) "100" } [10]=> array(3) { ["name"]=> string(6) "Easter" ["days"]=> string(197) "2026-04-02,2026-04-03,2026-04-04,2026-04-05,2026-04-06,2026-04-07,2026-04-08,2026-04-09,2026-04-10,2026-04-11,2026-04-12,2026-04-13,2026-04-14,2026-04-15,2026-04-16,2026-04-17,2026-04-18,2026-04-19" ["price"]=> string(3) "100" } [11]=> array(3) { ["name"]=> string(7) "May Day" ["days"]=> string(43) "2026-05-01,2026-05-02,2026-05-03,2026-05-04" ["price"]=> string(3) "110" } [12]=> array(3) { ["name"]=> string(16) "Summer half-term" ["days"]=> string(109) "2026-05-22,2026-05-23,2026-05-24,2026-05-25,2026-05-26,2026-05-27,2026-05-28,2026-05-29,2026-05-30,2026-05-31" ["price"]=> string(3) "110" } [13]=> array(3) { ["name"]=> string(6) "Summer" ["days"]=> string(516) "2026-07-17,2026-07-18,2026-07-19,2026-07-20,2026-07-21,2026-07-22,2026-07-23,2026-07-24,2026-07-25,2026-07-26,2026-07-27,2026-07-28,2026-07-29,2026-07-30,2026-07-31,2026-08-01,2026-08-02,2026-08-03,2026-08-04,2026-08-05,2026-08-06,2026-08-07,2026-08-08,2026-08-09,2026-08-10,2026-08-11,2026-08-12,2026-08-13,2026-08-14,2026-08-15,2026-08-16,2026-08-17,2026-08-18,2026-08-19,2026-08-20,2026-08-21,2026-08-22,2026-08-23,2026-08-24,2026-08-25,2026-08-26,2026-08-27,2026-08-28,2026-08-29,2026-08-30,2026-08-31,2026-09-01" ["price"]=> string(3) "120" } [14]=> array(3) { ["name"]=> string(16) "Autumn half-term" ["days"]=> string(120) "2026-10-23,2026-10-24,2026-10-25,2026-10-25,2026-10-26,2026-10-27,2026-10-28,2026-10-29,2026-10-30,2026-10-31,2026-11-01" ["price"]=> string(3) "110" } [15]=> array(3) { ["name"]=> string(10) "Christmas " ["days"]=> string(186) "2026-12-18,2026-12-19,2026-12-20,2026-12-21,2026-12-22,2026-12-23,2026-12-24,2026-12-25,2026-12-26,2026-12-27,2026-12-28,2026-12-29,2026-12-30,2026-12-31,2027-01-01,2027-01-02,2027-01-03" ["price"]=> string(3) "120" } If the requested dates are present in the price array i then need to extract the price for each day to put into an array to calculate the total cost if(in_array($rested, $prices) { array_push($cost, $prices[]['price']; } echo array_sum($cost); I have tried for loops, for each loops, array_diff, array_intersect and anything else i could think of put cant get it to work properly. Any help would me much appreciated The code i have put isnt the exact code i have now as i have been trying all day and its changed every time i've tried something else. The code i have right now only adds 2 prices to the cost array even though there more than 2 dates in the requested dates Please Help!! Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/ Share on other sites More sharing options...
mac_gyver Posted 9 hours ago Share Posted 9 hours ago for the posted information, the requested date range of 2025-05-06 to 2025-05-22 doesn't match any of the $seasonPrices data. It starts one day after the end of the May Day range and ends one day before the start of the Summer half-term range. it should use the base/default price for every day. since you are using a standard date format, you can directly perform date comparisons by order, as mentioned in the previous thread. you can directly compare a date to the $seasonPrices SeasonStart and SeasonEnd values to find if it is between a date range. i would write a function/class-method that accepts a date input, loops over the $seasonPrices data, returns the first price that is between the SeasonStart and SeasonEnd values, or returns zero (or some other easily detected value) if it reaches the end without finding a match. as a procedural function, something like - function get_price($date,$seasonPrices) { foreach($seasonPrices as $row) { // date between start and end if($row->SeasonStart <= $date && $date <= $row->SeasonEnd) { return $row->SeasonPrice; } } // no match return 0; } Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/#findComment-1653665 Share on other sites More sharing options...
Zane Posted 9 hours ago Share Posted 9 hours ago Your wording and examples are a bit all over the place, I believe you're trying to filter an array, and then manipulate any specific ones that match particular date. Right? So, if 5/5/25 is a day with a special rate, and is within the date range provided by the user, use the "special" price? You can use array_map to manipulate the items that match a specific date. An example of it being used for only one "special" date - May 5 2025 $dates = [ '2025-05-01', '2025-05-02', '2025-05-03', '2025-05-04', '2025-05-05', ]; $specialDate = '2025-05-05'; $specialPrice = 8.00; // The special price to apply ONLY on $specialDate $priceUpdateCallback = function ($item) use ($specialDate, $specialPrice) { $element = ["date" => $item, "price" => 10]; if ($item == $specialDate) { $element['date'] = $specialDate; $element['price'] = $specialPrice } return $element; }; $result = array_map('priceUpdateCallback', $dates); echo "<pre>", print_r($result), "</pre>"; Should give you something like Array ( .............Array ( [date] => 2025-05-05 [price] => 8 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/#findComment-1653666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.