Re: Head Twister: Need Help Please
| From: | David Otton | Date: | Sun, 20 Apr 2003 01:34:43 +0000 |
| Subject: | Re: Head Twister: Need Help Please | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-144514@lists.php.net to get a copy of this message | ||
On Sat, 19 Apr 2003 20:23:31 -0400, you wrote:
> I think I'm cross eyed after trying to write a script that would determine
>the day of the year (date("z") and then do the math to get the number. If
>you feel led to help out the fire department pleeeze do :-)... BTW, you
>can see the old script below working at the top of www.svfd.org
Here's a shot at a general solution, which should give you a hand. When
you're trying to map a repeating seqeunce (your shift pattern) onto a
longer seqeunce (the calender), mod (%) is your friend. My pattern
counts days, but changing it to count weeks is pretty simple.
There may be bugs (for a start, make sure you don't feed it calculate
dates older than start dates)
/* accepts a shift pattern as an array of text strings, and unix
* timestamps denoting the date the shift pattern begins, and the
* date which should be calculated for.returns the string that is
* "active" on that day */
function calculateShift($shiftpattern, $starttime, $calctime) {
/* calculate number of days between $starttime and $calctime */
$days = ((($calctime - $starttime) / 60) / 60) / 24;
return $shiftpattern[(int)$days % sizeof($shiftpattern)];
}
/* a shiftpattern array contains a series of text strings,
* each denoting a shift. */
$joe = array("6-2", "6-2", "6-2", "off", "off",
"2-10", "2-10", "2-10",
"off", "off", "10-6", "10-6", "10-6",
"off", "off");
$fred = array("2-10", "2-10", "2-10", "off",
"off", "10-6", "10-6",
"10-6", "off", "off", "6-2", "6-2",
"6-2", "off", "off");
$r = calculateShift($joe, strtotime("01 April 2003"), strtotime("04
April 2003"));
echo ("<p>On 4th April 2003, Joe will be on " . $r . " shift</p>");
$r = calculateShift($fred, strtotime("01 April 2003"), strtotime("08
April 2003"));
echo ("<p>On 8th April 2003, Fred will be on " . $r . " shift</p>");
$r = calculateShift($fred, strtotime("01 April 2003"), strtotime("25
December 2003"));
echo ("<p>On 25th December 2003, Fred will be on " . $r . "
shift</p>");