1

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
1
  • 2
    You need to sent your format to the function, now you are sending a string.. '%a' Commented Oct 27, 2015 at 10:58

1 Answer 1

3

'%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

Ah that works perfectly thank you!! I'll accept it when it lets me :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.