note 78119 added to function.time
| From: | JoshAbraham at osu1 dot php dot net | Date: | Fri, 28 Sep 2007 14:43:06 +0000 |
| Subject: | note 78119 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130882@lists.php.net to get a copy of this message | ||
When dealing with the results of the time function, taking the modulus (remainder) is often a good
way to find recurring information such as day of the week, week of the year, or month of the year.
In the example given below of a firefighter's shift, you could do the following to simplify the
code.
<?php
function whatShift() {
$referencePoint = mktime(7, 0, 0, 9, 11, 2004); // Sept 11, 2004 at 7AM started an A Shift.
//This is the where we divide the current time since reference by the amount of time in all shifts
//The result of this is the remainder.
$sinceReference = (time() - $referencePoint) % (60 * 60 * 24 * 3);
//The rest of the code can be basically the same so I shortened it here.
if ($sinceReference < 60 * 60 * 25) $shift = "A";
elseif ($sinceReference < 60 * 60 * 49) $shift = "B";
else $shift = "C";
return $shift;
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 66.170.97.28
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- https://master.php.net/note/edit/78119
Del: integrated -- https://master.php.net/note/delete/78119/integrated
Del: useless -- https://master.php.net/note/delete/78119/useless
Del: bad code -- https://master.php.net/note/delete/78119/bad+code
Del: spam -- https://master.php.net/note/delete/78119/spam
Del: non-english -- https://master.php.net/note/delete/78119/non-english
Del: in docs -- https://master.php.net/note/delete/78119/in+docs
Del: other reasons-- https://master.php.net/note/delete/78119
Reject -- https://master.php.net/note/reject/78119
Search -- https://master.php.net/manage/user-notes.php