note 57580 deleted from function.time by aidan

From: Date: Fri, 07 Oct 2005 23:53:17 +0000
Subject: note 57580 deleted from function.time by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96582@lists.php.net to get a copy of this message
Note Submitter: putaso Reason: useless ---- 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.

« previous php.notes (#96582) next »