I have a value in the database defined as mediumint(11). Currently it returns 3600 if I run a query.
This function here should convert that value to: 1 hr. But it doesn’t when I run the function. I get no value.
function strTime($s) {
$d = intval($s/86400);
$s -= $d*86400;
$h = intval($s/3600);
$s -= $h*3600;
$m = intval($s/60);
$s -= $m*60;
if ($d) $str = $d . 'd ';
if ($h) $str .= $h . 'h ';
if ($m) $str .= $m . 'm ';
if ($s) $str .= $s . 's';
return $str;
}
Pretty basic, must be something with the value in the db?
Code that calls this (Joomla specific):
$query = "SELECT streaming_limit FROM #__cc_users WHERE user_id=".$user->id;
$db->setQuery($query);
$steaming_limit = $db->loadResult(); //returns 3600
echo strTime($streaming_limit); //returns nothing
A more robust way to do this is PHP’s
DateTimeandDateIntervalclasses, which exist specifically for this sort of thing.The code below creates two DateTime objects, adds the specified number of seconds
to one object and then calculates the interval between the two.
At that point you can format the time difference however you’d like using
DateInterval::format