Seconds to human readable text

<?php /** * Seconds to human readable text * Eg: for 36545627 seconds => 1 year, 57 days, 23 hours and 33 minutes * * @return string Text */ function getHumanTime($seconds) { $units = array( 'year' => 365*86400, 'day' => 86400, 'hour' => 3600, 'minute' => 60, // 'second' => 1, ); $parts = array(); foreach ($units as $name => $divisor) { $div = floor($seconds / $divisor); if ($div == 0) continue; else if ($div == 1) $parts[] = $div.' '.$name; else $parts[] = $div.' '.$name.'s'; $seconds %= $divisor; } $last = array_pop($parts); if (empty($parts)) return $last; else return join(', ', $parts).' and '.$last; }
Seconds to human readable text

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.