Jump to content

checking to see if a date lands in a season


dadamssg87

Recommended Posts

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];
			}
		}
	}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.