note 59105 added to function.fmod
| From: | cory at lavacube dot net | Date: | Sat, 26 Nov 2005 08:38:46 +0000 |
| Subject: | note 59105 added to function.fmod | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99130@lists.php.net to get a copy of this message | ||
A more formal way for generating duration strings:
<?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 = floor( $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 );
}
?>
Simple use:
<?php
echo duration( mktime(0, 0, 0, 1, 1, date('Y')+1) - time(), 'Some fancy message to
output if duration is already met...' );
?>
Enjoy. :-)
----
Manual Page -- http://www.php.net/manual/en/function.fmod.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59105
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59105&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59105&report=yes
Search -- http://master.php.net/manage/user-notes.php