dadamssg87 Posted October 25, 2011 Share Posted October 25, 2011 I have a an array of dates, an array of normal weekly rates, and an array of seasonal rates. Items in the seasons array consist of: start, end, ongoing, and weekly rates. If the ongoing value is "Y" the season should apply to every year. If the value is "N" then the season should only last for the specified range. The below code is doing what i want except I just don't know how to incorporate the ongoing-ness of the seasonal rates. The last date in the dates array should affected by the second seasonal rates. Can't think of how to go about figuring this out. <?php $dates[] = "1/26/2010"; $dates[] = "4/13/2011"; $dates[] = "4/19/2034"; $normal_rates = array( 'monday' => 0.99, 'tuesday' => 0.99, 'wednesday' => 0.99, 'thursday' => 0.99, 'friday' => 0.99, 'saturday' => 1.50, 'sunday' => 1.50, ); $seasons[] = array( 'start' => "3/15/2011", 'end' => "4/25/2011", 'ongoing' => "Y", 'monday' => 4.99, 'tuesday' => 4.99, 'wednesday' => 4.99, 'thursday' => 4.99, 'friday' => 4.99, 'saturday' => 10.99, 'sunday' => 10.99, ); $seasons[] = array( 'start' => "3/15/2011", 'end' => "4/25/2011", 'ongoing' => "N", 'monday' => 1.99, 'tuesday' => 1.99, 'wednesday' => 1.99, 'thursday' => 1.99, 'friday' => 1.99, 'saturday' => 19.99, 'sunday' => 19.99, ); foreach($dates as $date) { $x_timestamp = strtotime($date); $day_of_the_week = strtolower(date("l", strtotime($date))); $date_rates[$date] = $normal_rates[$day_of_the_week]; foreach($seasons as $key => $value) { $s_timestamp = strtotime($seasons[$key]['start']); $e_timestamp = strtotime($seasons[$key]['end']); if($x_timestamp >= $s_timestamp && $x_timestamp <= $e_timestamp) //should have an or(||) in it for ongoing { $date_rates[$date] = $seasons[$key][$day_of_the_week]; } } } Quote Link to comment https://forums.phpfreaks.com/topic/249789-checking-to-see-if-a-date-lands-in-a-season/ Share on other sites More sharing options...
requinix Posted October 25, 2011 Share Posted October 25, 2011 Break the problem down into pieces. There's only really one: checking if a date is within a season. Once you have that the rest is straightforward. function season_rate($date, $season) { $date = strtotime($date); // easier to work with this /* $start = strtotime(start of season); same for $end and the end of the season if (season is ongoing) { // use $date year for any ranges $start = mktime(0, 0, 0, $start month, $start day, $date year); same for $end } if ($date is inside the $start-$end range) { return the rate in this $season for the $date day of the week } else { return null; } */ } With that done, for each date 1. For each season, - a. Check for a rate - b. If you get one, use that value 2. Since no seasons match, use the normal rate for that day But are you going to be working with date ranges beyond 2037? Quote Link to comment https://forums.phpfreaks.com/topic/249789-checking-to-see-if-a-date-lands-in-a-season/#findComment-1282150 Share on other sites More sharing options...
dadamssg87 Posted October 25, 2011 Author Share Posted October 25, 2011 that took a lot of rereading but i think i understand now. i appreciate it. I was trying to figure out how to do it if the range spanned two years(ex. start: 12/25/2010 End: 2/14/2011) but i think it would make more sense and it be easier if i just make sure if they user checks the ongoing box that the validation throws an error if the two dates aren't in the same year. Quote Link to comment https://forums.phpfreaks.com/topic/249789-checking-to-see-if-a-date-lands-in-a-season/#findComment-1282163 Share on other sites More sharing options...
dadamssg87 Posted October 25, 2011 Author Share Posted October 25, 2011 oh and what does it matter if the dates are beyond 2037? Quote Link to comment https://forums.phpfreaks.com/topic/249789-checking-to-see-if-a-date-lands-in-a-season/#findComment-1282164 Share on other sites More sharing options...
requinix Posted October 25, 2011 Share Posted October 25, 2011 Ranges can span years if you use strtotime() to actually compare dates. Which you have to anyways because you can't compare MM/DD/YYYY date strings. 32-bit PHP only supports dates up into January 2038. Any further and you end up wrapping around to December 1901. Quote Link to comment https://forums.phpfreaks.com/topic/249789-checking-to-see-if-a-date-lands-in-a-season/#findComment-1282185 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.