Andy2024 Posted Sunday at 06:58 PM Share Posted Sunday at 06:58 PM 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 Sunday at 09:39 PM Share Posted Sunday at 09:39 PM 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 Sunday at 09:41 PM Share Posted Sunday at 09:41 PM 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...
Andy2024 Posted 22 hours ago Author Share Posted 22 hours ago I'll just put what i am trying to achieve instead of the code i am using I have 2 arrays with stored days and prices # Standard Days Array ( [0] => stdClass Object ( [name] => Standard Weekday [price] => 60 ) [1] => stdClass Object ( [name] => Standard Weekend [price] => 80 ) ) # Special Days Array ( [0] => Array ( [start] => 2025-02-21 [end] => 2025-03-03 [price] => 100 ) [1] => Array ( [start] => 2025-04-11 [end] => 2025-04-27 [price] => 100 ) [2] => Array ( [start] => 2025-05-02 [end] => 2025-05-05 [price] => 110 ) [3] => Array ( [start] => 2025-05-23 [end] => 2025-06-01 [price] => 110 ) [4] => Array ( [start] => 2025-07-18 [end] => 2025-09-01 [price] => 120 ) [5] => Array ( [start] => 2025-10-24 [end] => 2025-11-02 [price] => 110 ) [6] => Array ( [start] => 2025-12-19 [end] => 2026-01-04 [price] => 120 ) ) I want to get the prices from those arrays for each day from the selected dates, so from 1st May 2025 to 8th May 2025 would look like this # Selected Days Array ( [0] => 2025-05-01 [1] => 2025-05-02 [2] => 2025-05-03 [3] => 2025-05-04 [4] => 2025-05-05 [5] => 2025-05-06 [6] => 2025-05-07 [7] => 2025-05-08 ) so for each of the Selected Days if the date is not in the Special Days array it will give me the price of the standard weekday or weekend, If it is in the special date it will give me that instead. Because there are no special dates in the selected days it would calculate (5x60)+(2x80) so give a total of 460. Its when there is special dates where i am having the issue, how can i check each selected day in the special days array and get the price for that array Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/#findComment-1653671 Share on other sites More sharing options...
Andy2024 Posted 22 hours ago Author Share Posted 22 hours ago So far this is what code I have if(!empty($standardRates)) { foreach($standardRates as $row) { if($row->SeasonName == 'Standard Weekday') { $standardRate['Weekday'] = $row->SeasonPrice; } if($row->SeasonName == 'Standard Weekend') { $standardRate['Weekend'] = $row->SeasonPrice; } } } if(!empty($setPrices)) { $i = 0; foreach($setPrices as $row) { $specialRate[$i]['start'] = $row->SeasonStart; $specialRate[$i]['end'] = $row->SeasonEnd; $specialRate[$i]['price'] = $row->SeasonPrice; $i++; } } $cost = array(); foreach($flatRequest as $key => $val) { for($i=0; $i<count($specialRate); $i++) { if(in_array($val, Cal::bookedArray($specialRate[$i]['start'], $specialRate[$i]['end']))) { array_push($cost, $specialRate[$i]['price']); } } if(empty($specialRate)) { if(date('l', strtotime($val)) == 'Saturday' || date('l', strtotime($val)) == 'Sunday') { array_push($cost, $standardRate['Weekend']); } else { array_push($cost, $standardRate['Weekday']); } } } This doesn't work though, $cost is empty Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/#findComment-1653673 Share on other sites More sharing options...
mac_gyver Posted 21 hours ago Share Posted 21 hours ago you are constantly changing data types, names, and adding features. this makes writing code extremely wasteful. you need to define everything possible, before you write any code. you also need to define what inputs you have for any operation, what processing you are going to do based on those inputs, and what result or output you are going to produce. as to a simple, straightforward solution, i recommend that you read the suggestions in the previous thread, about building an array with the dates as the array index and the values being whatever you are trying to produce. in that thread, the array was for the special events for the calendar being displayed. in this thread, the array is for the requested date range and the price for each day in that range. the inputs to this code are the start and end dates of the request, the standard week day and week end prices, and the seasonal pricing. you would start by creating an array using the request range of dates as the array index. you would fill in the array with the week day and week end prices. you would then replace any price that matches the seasonal pricing data. when you are done, you will have an array with all the days of the request as the index, and the price for each day. you can then just sum the prices to get the total. here's a procedural function that gets the seasonal prices, given the request start and end date - // get seasonal prices between start and end date function get_prices($start,$end,$seasonPrices) { $result = []; foreach($seasonPrices as $row) { // keep entries that match the requested start/end // SeasonEnd >= start AND end >= SeasonStart if($row->SeasonEnd >= $start && $end >= $row->SeasonStart) { // expand this entry and only keep the values between the start and end (for the case of spanning the start or end date) $entry = Cal::bookedArray($row->SeasonStart, $row->SeasonEnd); foreach($entry as $date) { // if date between start and end, keep it if($start <= $date && $date <= $end) { $result[$date] = $row->SeasonPrice; } } } } return $result; } you can use array_replace() with the array you are building and the array returned by the above function to replace the standard prices in that array with the seasonal prices. Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/#findComment-1653674 Share on other sites More sharing options...
mac_gyver Posted 9 hours ago Share Posted 9 hours ago here's a tested example showing the suggested method - <?php // fake a request start and end date - 1st May 2025 to 8th May 2025 $start_date = "2025-05-01"; $end_date = "2025-05-08"; // exclude check-out date from length of stay/pricing $end = new DateTime($end_date); $end = $end->modify( '-1 day' ); $end_date = $end->format("Y-m-d"); // define default weekday and weekend prices $default_price = []; $default_price['wd'] = (object)['name'=>'Standard Weekday','price'=>'60']; $default_price['we'] = (object)['name'=>'Standard Weekend','price'=>'80']; // define seasonal prices $seasonPrices = []; $seasonPrices[] = (object)["SeasonName"=>"Spring half-term", "SeasonStart"=>'2025-02-21', "SeasonEnd"=>'2025-03-03', "SeasonPrice"=>'100']; $seasonPrices[] = (object)["SeasonName"=>"Easter", "SeasonStart"=>'2025-04-11', "SeasonEnd"=>'2025-04-27', "SeasonPrice"=>'100']; $seasonPrices[] = (object)["SeasonName"=>"May Day", "SeasonStart"=>'2025-05-02', "SeasonEnd"=>'2025-05-05', "SeasonPrice"=>'110']; $seasonPrices[] = (object)["SeasonName"=>"Summer half-term", "SeasonStart"=>'2025-05-23', "SeasonEnd"=>'2025-06-01', "SeasonPrice"=>'110']; $seasonPrices[] = (object)["SeasonName"=>"Summer", "SeasonStart"=>'2025-07-18', "SeasonEnd"=>'2025-09-01', "SeasonPrice"=>'120']; $seasonPrices[] = (object)["SeasonName"=>"Autumn half-term", "SeasonStart"=>'2025-10-24', "SeasonEnd"=>'2025-11-02', "SeasonPrice"=>'110']; $seasonPrices[] = (object)["SeasonName"=>"Christmas", "SeasonStart"=>'2025-12-19', "SeasonEnd"=>'2026-01-04', "SeasonPrice"=>'120']; // start by creating an array of request dates filled with the default weekday price $request = array_fill_keys(Cal::bookedArray($start_date, $end_date), $default_price['wd']->price); // get the weekend prices between the request start and end dates $weekend_prices = get_we_prices($start_date,$end_date,$default_price); // put the weekend prices into the array $request = array_replace($request,$weekend_prices); // get the seasonal prices between the request start and end dates $season_prices = get_prices($start_date,$end_date,$seasonPrices); // put the seasonal prices into the array $request = array_replace($request,$season_prices); // examine the result echo '<pre>'; print_r($request); echo '</pre>'; echo "Total: " . array_sum($request); // get the weekend prices between the request start and end dates function get_we_prices($start_date,$end_date,$default_price) { $dates = []; // find 1st Sat of the start date $date = date('Y-m-d',strtotime("first sat of $start_date")); // generate all the Saturdays between the start and end dates $start = new DateTime($date); $end = new DateTime($end_date); $end = $end->modify( '+1 day' ); // include the end point $interval = new DateInterval('P7D'); // 7 days $daterange = new DatePeriod($start, $interval ,$end); foreach($daterange as $date){ $dates[$date->format("Y-m-d")] = $default_price['we']->price; } // same as above but for Sundays // find 1st Sun of the start date $date = date('Y-m-d',strtotime("first sun of $start_date")); // generate all the Sundays between the start and end dates $start = new DateTime($date); $end = new DateTime($end_date); $end = $end->modify( '+1 day' ); // include the end point $interval = new DateInterval('P7D'); // 7 days $daterange = new DatePeriod($start, $interval ,$end); foreach($daterange as $date){ $dates[$date->format("Y-m-d")] = $default_price['we']->price; } return $dates; } // get the seasonal prices between the request start and end dates function get_prices($start,$end,$seasonPrices) { $result = []; foreach($seasonPrices as $row) { // keep entries that match the requested start/end // SeasonEnd >= start AND end >= SeasonStart if($row->SeasonEnd >= $start && $end >= $row->SeasonStart) { // expand this entry and only keep the values between the start and end (for the case of spanning the start or end date) $entry = Cal::bookedArray($row->SeasonStart, $row->SeasonEnd); foreach($entry as $date) { // if date between start and end, keep it if($start <= $date && $date <= $end) { $result[$date] = $row->SeasonPrice; } } } } return $result; } class cal { // produce a date range from start to end public static function bookedArray($start,$end) { $start = new DateTime($start); $end = new DateTime($end); $end = $end->modify( '+1 day' ); // include the end point $interval = new DateInterval('P1D'); // 1 day $daterange = new DatePeriod($start, $interval ,$end); $dates = array(); foreach($daterange as $date){ $dates[] = $date->format("Y-m-d"); } return $dates; } } Quote Link to comment https://forums.phpfreaks.com/topic/327663-date-range-and-arrays/#findComment-1653690 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.