note 57580 added to function.time
| From: | putaso at osu1 dot php dot net | Date: | Fri, 07 Oct 2005 22:55:10 +0000 |
| Subject: | note 57580 added to function.time | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96578@lists.php.net to get a copy of this message | ||
This is an improved version of the seconds_to_time function by ap.
if you set to TRUE the seconds argument, it will return a the time in the format:
20 days, 15 hours, 10 minutes, 5 seconds.
<?php
error_reporting(E_ALL);
function seconds_to_time($seconds, $return_str=FALSE) {
if ( !is_numeric($seconds) ) {
trigger_error("\$seconds is not a number", E_USER_WARNING);
return FALSE;
}
$days = $hours = $minutes = 0;
while ($seconds >= 60) {
if ($seconds >= 86400) {
// there is more than 1 day
$days = floor($seconds / 86400);
$seconds = $seconds - ($days * 86400);
} elseif ($seconds >= 3600) {
$hours = floor($seconds / 3600);
$seconds = $seconds - ($hours * 3600);
} elseif ($seconds >= 60) {
$minutes = floor($seconds / 60);
$seconds = $seconds - ($minutes * 60);
}
}
if ($return_str) {
$result = '';
if ( isset($days) && !empty($days) ) {
$result .= ($days == 1) ? $days.' day, ' : $days.' days, ';
}
if ( $result != '' || $hours > 0 ) {
$result .= ($hours == 1) ? $hours.' hour, ' : $hours.' hours, ';
}
if ( $result != '' || $minutes > 0 ) {
$result .= ($minutes == 1) ? $minutes.' minute, ' : $minutes.' minutes, ';
}
$result .= ($seconds == 1) ? $seconds.' second.' : $seconds.' seconds.';
return $result;
} else {
return array('days' => $days, 'hours' => $hours, 'minutes'
=> $minutes, 'seconds' => $seconds);
}
}
//TEST
echo seconds_to_time(5, TRUE)."\n"; //seconds
echo seconds_to_time(10*60, TRUE)."\n"; //minutes
echo seconds_to_time(10*60+ 5, TRUE)."\n"; //minutes seconds
echo seconds_to_time(15*60*60, TRUE)."\n"; //hours
echo seconds_to_time(15*60*60 + 10*60, TRUE)."\n"; //hours minutes
echo seconds_to_time(15*60*60 + 10*60 + 5, TRUE)."\n"; //hours minutes seconds
echo seconds_to_time(15*60*60 + 5, TRUE)."\n"; //hours seconds
echo seconds_to_time(20*60*60*24, TRUE)."\n"; //days
echo seconds_to_time(20*60*60*24 + 15*60*60, TRUE)."\n"; //days hours
echo seconds_to_time(20*60*60*24 + 15*60*60 + 10*60, TRUE)."\n"; //days hours minutes
echo seconds_to_time(20*60*60*24 + 15*60*60 + 10*60 + 5, TRUE)."\n"; //days hours minutes
seconds
echo seconds_to_time(20*60*60*24 + 10*60 + 5, TRUE)."\n"; //days minutes seconds
echo seconds_to_time(20*60*60*24 + 15*60*60 + 5, TRUE)."\n"; //days hours seconds
echo seconds_to_time(20*60*60*24 + 10*60, TRUE)."\n"; //days minutes
echo seconds_to_time(20*60*60*24 + 5, TRUE)."\n"; //days seconds
echo seconds_to_time('', TRUE)."\n"; //Error
echo seconds_to_time(null, TRUE)."\n"; //Error
echo seconds_to_time('a', TRUE)."\n"; //Error
echo seconds_to_time(0, TRUE)."\n"; //Not Error
?>
Which will return:
5 seconds.
10 minutes, 0 seconds.
10 minutes, 5 seconds.
15 hours, 0 minutes, 0 seconds.
15 hours, 10 minutes, 0 seconds.
15 hours, 10 minutes, 5 seconds.
15 hours, 0 minutes, 5 seconds.
20 days, 0 hours, 0 minutes, 0 seconds.
20 days, 15 hours, 0 minutes, 0 seconds.
20 days, 15 hours, 10 minutes, 0 seconds.
20 days, 15 hours, 10 minutes, 5 seconds.
20 days, 0 hours, 10 minutes, 5 seconds.
20 days, 15 hours, 0 minutes, 5 seconds.
20 days, 0 hours, 10 minutes, 0 seconds.
20 days, 0 hours, 0 minutes, 5 seconds.
<br />
<b>Warning</b>: $seconds is not a number in <b>file.php</b> on line
<b>6</b><br />
<br />
<b>Warning</b>: $seconds is not a number in <b>file.php</b> on line
<b>6</b><br />
<br />
<b>Warning</b>: $seconds is not a number in <b>file.php</b> on line
<b>6</b><br />
0 seconds.
----
Manual Page -- http://www.php.net/manual/en/function.time.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57580
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57580&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57580&report=yes
Search -- http://master.php.net/manage/user-notes.php