note 90338 added to function.time
| From: | russell at greendotsoftware dot com | Date: | Thu, 16 Apr 2009 22:21:37 +0000 |
| Subject: | note 90338 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-153410@lists.php.net to get a copy of this message | ||
<?php
// In reply to jon at freilich dot com
// Some Fire Departments have 3 shifts
// Each shift works for 24 hours, and works 5, 24 hour shifts berfore their break.
// I was asked to provide the function so that their online calendar would output
// What shift was on duty from Jan 2001 until, well forever.
// this function is for wierd rotating schedules that have a set pattern, like Fire Departments
// You need to provide the attributes for this function to work
// $day = what day of the month, for example 9
// $date = use time() to check today or convert the date you want to check into seconds by using
the function mktime(7,0,0,1,1,2009);
function whatShift($day, $date)
{
$month = date("m", $date); // create the month by converting the attribute $date and
getting what month you want.
$year = date("Y", $date); // create the year by converting the attribute $date and
getting what you you want.
$date_in_question = mktime(7,0,0,$month, $day, $year);
// HINT you need to use ROUND, otherwise, you will spend LOTS of time wondering why it does
not work right!
// What happens here is that we are doing the math and using modulus to find out what shift is
on duty.
// If the answer is 0, then that shift is on duty.
// If you do not know what Modulus is, it gives you the remainder after something is divided
by another
// We use 15 because there is always 15 days between the first day of the set for each shift
// and the start of the next set
// A shift
$ashift1 = (round(($date_in_question - mktime(7,0,0,1,8,2001)) / 86400) % 15);
$ashift2 = (round(($date_in_question - mktime(7,0,0,1,10,2001)) / 86400) % 15);
$ashift3 = (round(($date_in_question - mktime(7,0,0,1,12,2001)) / 86400) % 15);
$ashift4 = (round(($date_in_question - mktime(7,0,0,1,14,2001)) / 86400) % 15);
$ashift5 = (round(($date_in_question - mktime(7,0,0,1,16,2001)) / 86400) % 15);
// B shift
$bshift1 = (round(($date_in_question - mktime(7,0,0,1,13,2001)) / 86400) % 15);
$bshift2 = (round(($date_in_question - mktime(7,0,0,1,15,2001)) / 86400) % 15);
$bshift3 = (round(($date_in_question - mktime(7,0,0,1,17,2001)) / 86400) % 15);
$bshift4 = (round(($date_in_question - mktime(7,0,0,1,19,2001)) / 86400) % 15);
$bshift5 = (round(($date_in_question - mktime(7,0,0,1,21,2001)) / 86400) % 15);
// C shift
$cshift1 = (round(($date_in_question - mktime(7,0,0,1,3,2001)) / 86400) % 15);
$cshift2 = (round(($date_in_question - mktime(7,0,0,1,5,2001)) / 86400) % 15);
$cshift3 = (round(($date_in_question - mktime(7,0,0,1,7,2001)) / 86400) % 15);
$cshift4 = (round(($date_in_question - mktime(7,0,0,1,9,2001)) / 86400) % 15);
$cshift5 = (round(($date_in_question - mktime(7,0,0,1,11,2001)) / 86400) % 15);
// A shift
// If any one of these equals 0, then A shift is on duty.
if (($ashift1 == 0) || ($ashift2 == 0) || ($ashift3 == 0) || ($ashift4 == 0) || ($ashift5 ==
0))
{
$message = '<div style="background-color: #060; color: #FFF; text-align:
center;">'.$day.' is an A shift day</div>';
}
// B shift
// If any one of these equals 0, then B shift is on duty.
if (($bshift1 == 0) || ($bshift2 == 0) || ($bshift3 == 0) || ($bshift4 == 0) || ($bshift5 ==
0))
{
$message = '<div style="background-color: #060; color: #FFF; text-align:
center;">'.$day.' is a B shift day</div>';
}
// C shift
// If if any one of these equals 0, then C shift is on duty.
if (($cshift1 == 0) || ($cshift2 == 0) || ($cshift3 == 0) || ($cshift4 == 0) || ($cshift5 ==
0))
{
$message = '<div style="background-color: #060; color: #FFF; text-align:
center;">'.$day.' is a C shift day</div>';
}
// This is just in case something wierd happens
if (empty($message))
{
$message = "ERROR!";
}
return $message;
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 68.13.171.69
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/90338
Del: integrated -- https://master.php.net/note/delete/90338/integrated
Del: useless -- https://master.php.net/note/delete/90338/useless
Del: bad code -- https://master.php.net/note/delete/90338/bad+code
Del: spam -- https://master.php.net/note/delete/90338/spam
Del: non-english -- https://master.php.net/note/delete/90338/non-english
Del: in docs -- https://master.php.net/note/delete/90338/in+docs
Del: other reasons-- https://master.php.net/note/delete/90338
Reject -- https://master.php.net/note/reject/90338
Search -- https://master.php.net/manage/user-notes.php