Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:24:53+00:00 2026-05-26T15:24:53+00:00

In this website I’ve found a function to convert seconds in an human readable

  • 0

In this website I’ve found a function to convert seconds in an human readable format, like this:

3 weeks, 2 days, 1 hour, 27 minutes, 52 seconds

I want to translate it in italian, so I just translated array keys. The function now is this

function secondsToHumanReadable($secs) {

$units = array(
'settimane' => 604800,
'giorni'    =>  86400,
'ore'       =>   3600,
'minuti'    =>     60,
'secondi'   =>      1
);

foreach ( $units as &$unit ) {
    $quot  = intval($secs / $unit);
    $secs -= $quot * $unit;
    $unit  = $quot;
}

return $units;
}

It works pretty well, but there’s a little problem: in english all the plurals ends with one letter less, but unfortunately in italian it’s not the same, as you can see below.

English             Italian    
- weeks, week       - settimane, settimana
- days, day         - giorni, giorno
- hours, hour       - ore, ora
- minutes, minute   - minuti, minuto
- seconds, second   - secondi, secondo

I want to find a solution to print singular keys when the values are == 1.

I was thinking that I could merge the array with another array that have singular keys, using array_combine().

$singular_units = array(
'settimana',
'giorno',
'ora',
'minuto',
'secondo'
);

print_r(array_combine( $singular_units, $units ));

/* print_r:
Array
(
    [settimana] => 604800
    [giorno] => 86400
    [ora] => 3600
    [minuto] => 60
    [secondo] => 1
)
*/

The array above is what I need, but I’m not able to use it, since I just cannot use another foreach.

$seconds = 12345*60; // just an example
$units = secondsToHumanReadable($seconds);              
$time_string = '';
foreach ($units as $u => $v)
    if (!empty($v))
        $time_string.= $v.' '.$u.', ';
echo substr($time_string, 0, -2);
// 1 settimane, 1 giorni, 13 ore, 45 minuti
// this echo is not correct :( is expected to be like this:
// 1 settimana, 1 giorno, 13 ore, 45 minuti

How could I implement the singular words?

Any help is really appreciated! Thank you so much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:24:54+00:00Added an answer on May 26, 2026 at 3:24 pm

    You can implement them any way you like, IMHO preferably something not like the current solution which lacks clarity and readability (at least that’s how the local-var-mutation-with-refs-and-variable-ping-pong looks like to me).

    Just one possible solution:

    $input = 12345 * 60;
    
    $units = array(
        604800 => array('settimana', 'settimane'),
        86400 => array('giorno', 'giorni'),
        // etc
    );
    
    $result = array();
    foreach($units as $divisor => $unitName) {
        $units = intval($input / $divisor);
        if ($units) {
            $input %= $divisor;
            $name = $units == 1 ? $unitName[0] : $unitName[1];
            $result[] = "$units $name";
        }
    }
    
    echo implode(', ', $result);
    

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this website iloveforwards.com created using drupal. I would like to have a
I stumbled upon this website: http://www.liptongreenmint.ro/ I like their small and simple slideshow with
use this website a lot but first time posting. My program creates a number
this is my first post on this website, but I'm all the time getting
Just came across this website . Feature 9 is memory management and they claim
The people on this website seem to know everything so I figured I would
So I have this website that will be accessed via an SSL connection (
According to this website , you can change to command key sequence used by
So I have this website that I'm building and they are wanting to be
Maybe a FAQ at this website. I have a TableViewController that holds a form.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.