Below is my function that will take a timestamp and tell you the time that has passed from now in the format of 23 days 3 hours 4 minutes 6 seconds
The main problem is on my site I use mysql's DATETIME instead of TIMESTAMP so to use this function I must convert my datetime from mysql to a timestamp and then run it through my function.
So I am curious, is there a better way to do this, on some pages where I have 100 mysql results, PHP must convert 100 dates to timestamps and then run this on 100 of them.
I am just wondering if there is a better performance method, and please don't recommend all the PHP frameworks (zend, etc.)
Appreciate any tips/help
function duration($timestamp) {
$years = floor($timestamp / (60 * 60 * 24 * 365));
$timestamp %= 60 * 60 * 24 * 365;
$weeks = floor($timestamp / (60 * 60 * 24 * 7));
$timestamp %= 60 * 60 * 24 * 7;
$days = floor($timestamp / (60 * 60 * 24));
$timestamp %= 60 * 60 * 24;
$hrs = floor($timestamp / (60 * 60));
$timestamp %= 60 * 60;
$mins = floor($timestamp / 60);
$secs = $timestamp % 60;
$str = "";
if ($years == 1) {
$str .= "{$years} year ";
}elseif ($years > 1) {
$str .= "{$years} yearss ";
}
if ($weeks == 1) {
$str .= "{$weeks} week ";
}elseif ($weeks > 1) {
$str .= "{$weeks} weeks ";
}
if ($days == 1) {
$str .= "{$days} day ";
}elseif ($days > 1) {
$str .= "{$days} days ";
}
if ($hrs == 1) {
$str .= "{$hrs} hour ";
}elseif ($hrs > 1) {
$str .= "{$hrs} hours ";
}
if ($mins == 1) {
$str .= "{$mins} minute ";
}elseif ($mins > 1) {
$str .= "{$mins} minutes ";
}
if ($mins < 1 && $secs >= 1) {
$str .= "{$secs} seconds ";
}
return $str;
}