Hi all,
I've been trying to create a friendly time function for a while, it's purpose should be quite obiovus from the code below. Currently, it outputs "expired" for everything.
I am so utterly confused from visualising the code and trying to figure out where the times lie in relation to each other, so I would really appreciate if some good soul could come along and make any suggestions.
public function generateFriendlyTime($time)
{
if(!is_numeric($time))
{
$time = strtotime($time);
}
$current_time = time();
if($time < $current_time)
{
return 'expired';
}
//If less than a minute
elseif($time < ($current_time + 60))
{
return "< " . 1 . "m";
}
//If less than an hour
elseif($time < ($current_time + 3600))
{
return round(($time - $current_time) / 60) . "mins";
}
//If less than a day
elseif($time < ($current_time + 86400))
{
return round(($time - $current_time) / 3600) . "h";
}
//If less than a week
elseif($time <= ($current_time + 604800))
{
return round(($time - $current_time) / 86400) . "d";
}
//If less than a month
elseif($current_time > 604800)
{
return round(($time - $current_time) / 604800) . "w";
}
else
{
return date('j F h:ia', $time);
}
}