note 28030 deleted from language.operators.arithmetic by sniper
| From: | sniper@php.net | Date: | Mon, 13 Oct 2003 00:08:46 +0000 |
| Subject: | note 28030 deleted from language.operators.arithmetic by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58391@lists.php.net to get a copy of this message | ||
Note Submitter: jim_myhrberg@hotmail.com
----
i think they explain the Modulus (%) operator a little bit weird... if you don't already know
how it works it can be really hard to find out... so here's the basics of the Modulus operator
if anyone whould like to know...
"Remainder of $a divided by $b.", i think that sounds a little bit weird, and also a
little wrong commpared to what it actually does if you're just tring to use logic... :P
12 % 5 = 2
12 % 10 = 2
now those two look weird if you don't know how it works. what the % operator does is that it
takes 5 sees how many times it will fit in 12. and 5 here only fits in 12 two times, so the total is
10, then 10 is removed from 12, and what's left is 2. and for 12 % 10 it's pretty simple,
cause 10 only fitts in 12 one time, and the final result is 2.
this kinda stuff is verry handy when you're doing time calculations. for example, if you got a
unix time stamp in seconds, and you wanna know how many hours in to the current day it is, you can
do something like this...
$time = time();
$hour = floor( ($time % 86400) / 3600);
so, first off we $time mod time with 86400 (which is the number of seconds in a day (24 hours)),
that will only leave the number of seconds in to the current day, and we divide that with 3600
(which is the number of seconds in one hour), and since the whole thing is within a floor function
it will end up returning only full hours.
i hope this will help some people and clarify how the % operator works :)
cya