I have a function to deal with the "s" on the end of plurals and another to convert a total of seconds into something more human intuitive.
I'm trying to use the plurals function inside the secondstotime so that it will say "1 day" and "10 days" for example. Notice the "s" on the end.
function plurals($number)
{
if ($number == 1)
{
return '';
}
else
{
return 's';
}
}
function secondstotime($seconds)
{
$dtF = new DateTime("@0");
$dtT = new DateTime("@$seconds");
if ($seconds < 60)
{
return $dtF->diff($dtT)->format('%s seconds');
}
else if ($seconds < 3600)
{
return $dtF->diff($dtT)->format('%i minutes');
}
else if ($seconds < 86400)
{
return $dtF->diff($dtT)->format('%h hours, %i minutes');
}
else if ($seconds < 31536000)
{
return $dtF->diff($dtT)->format('%a days');
}
else
{
return $dtF->diff($dtT)->format('%y years, %a days');
}
}
How do I do this? I've tried using code like this but it doesn't work:
return $dtF->diff($dtT)->format('%a day'.plurals('%a'));
asked Oct 27, 2015 at 10:55
-
2You need to sent your format to the function, now you are sending a string.. '%a'Naruto– Naruto2015年10月27日 10:58:54 +00:00Commented Oct 27, 2015 at 10:58
1 Answer 1
'%a'
is not a number, try this
return $dtF->diff($dtT)->format('%a day'.plurals($dtF->diff($dtT)->format('%a')));
answered Oct 27, 2015 at 10:59
1 Comment
Amy Neville
Ah that works perfectly thank you!! I'll accept it when it lets me :)
lang-php