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 6975083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:19:22+00:00 2026-05-27T17:19:22+00:00

Stuck trying to figure out the best algorithm here. I have this simple function

  • 0

Stuck trying to figure out the best algorithm here. I have this simple function where the input is seconds:

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 . 'day ';
  if ($h) $str .= $h . 'hr ';
  if ($m) $str .= $m . 'min ';
  if ($s) $str .= $s . 'sec';

  return $str;
}

I like this because it will only show what is needed. For example if the seconds are at 3600 the output of this function will be “1 hr”.

My issue is that in some cases the seconds will be 0 (when they start using the product). How can I show 0 secs in those cases but in other times like the example above I don’t want it to say “1 hr 0 sec”. I’m struggling to think of a way to do this.

  • 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-27T17:19:22+00:00Added an answer on May 27, 2026 at 5:19 pm

    Just check to see if the string is still empty after all four if statements have run. You also have an error where you append data to an uninitiated string if $d is 0; you should instead initialize $str explicitly and make all four if statements append.

    $str = "";
    
    if ($d) $str .= $d . 'day ';
    if ($h) $str .= $h . 'hr ';
    if ($m) $str .= $m . 'min ';
    if ($s) $str .= $s . 'sec';
    
    return $str ? $str : "0 sec";
    

    That is, “if we have seconds, or the string is still empty, show the number of seconds”.

    You should be expanding on each if statements to allow for pluralization as well, and possibly ignoring only the leading/trailing zeros instead of all zeros.

    What I mean is that these examples look weird:

    2 day 5 min
    5 day 1 sec
    1 hr 5 sec

    You probably want the zeros for the intermediate units:

    2 day 0 hr 5 min
    5 day 0 hr 0 min 1 sec
    1 hr 0 min 5 sec

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

Sidebar

Related Questions

Im stuck, been trying to figure this out for 2 hours now. I have
I'm stuck trying to figure this one out.. We currently have a date criteria
I've been trying to figure out the best way to model this entity, and
I am trying to figure out the best way to parse this comma delimited
I'm stuck trying to figure out if this is possible. Using the blog example
I've been stuck trying to figure out why a counter cache on my (parent)
I've been stuck trying to arrive at a best solution for this for a
I am currently trying to figure out what the best way is to create
I am stuck trying to figure out how to initiate a WSDL connection with
I'm trying to automate file comment headers. I'm stuck trying to figure out how

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.