note 59130 added to function.fmod
| From: | SnakeEater251 at osu1 dot php dot net | Date: | Sun, 27 Nov 2005 06:16:10 +0000 |
| Subject: | note 59130 added to function.fmod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99170@lists.php.net to get a copy of this message | ||
Note on the code given by cory at lavacube dot net.
You will recieve better results by not using floor and using round instead. As you continue
increasing to larger amounts of time you will notice that the outputted time is off by large
amounts.
so instead of $temp = floor( $int_seconds / $length );
we would use $temp = round( $int_seconds / $length );
<?php
function duration( $int_seconds=0, $if_reached=null )
{
$key_suffix = 's';
$periods = array(
'year' => 31556926,
'month' => 2629743,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1
);
// used to hide 0's in higher periods
$flag_hide_zero = true;
// do the loop thang
foreach( $periods as $key => $length )
{
// calculate
$temp = round( $int_seconds / $length );
// determine if temp qualifies to be passed to output
if( !$flag_hide_zero || $temp > 0 )
{
// store in an array
$build[] = $temp.' '.$key.($temp!=1?'s':null);
// set flag to false, to allow 0's in lower periods
$flag_hide_zero = false;
}
// get the remainder of seconds
$int_seconds = fmod($int_seconds, $length);
}
// return output, if !empty, implode into string, else output $if_reached
return ( !empty($build)?implode(', ', $build):$if_reached );
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.fmod.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59130
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59130&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59130&report=yes
Search -- http://master.php.net/manage/user-notes.php