note 28030 added to language.operators.arithmetic

From: Date: Mon, 30 Dec 2002 15:53:56 +0000
Subject: note 28030 added to language.operators.arithmetic
Groups: php.notes 
Request: Send a blank email to php-notes+get-41472@lists.php.net to get a copy of this message
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 -- http://www.php.net/manual/en/language.operators.arithmetic.php http://master.php.net/manage/user-notes.php?action=edit+28030 http://master.php.net/manage/user-notes.php?action=delete+28030 http://master.php.net/manage/user-notes.php?action=reject+28030

« previous php.notes (#41472) next »