note 78119 deleted from function.time by cmb

From: Date: Sun, 01 Aug 2021 14:55:49 +0000
Subject: note 78119 deleted from function.time by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-216707@lists.php.net to get a copy of this message
Note Submitter: Josh Abraham ---- 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; } ?>

« previous php.notes (#216707) next »