• 0

Changing seconds into days, hours, minutes, seconds


Question

Hey

Ok, so I'm going to be recording some time, in seconds. What I need to do is convert that time, in seconds, into a more viewable display of Days, Hours, Minutes and Seconds. I know how to get the number of days. seconds / 86400. But how do I convert the remaining time into hours, and then into seconds?

Thanks for any help :)

16 answers to this question

Recommended Posts

  • 0

Well do it on paper first.

Say you have 1161 seconds.

Divide that by 60 to get it in minutes.

So we have 19.35 minutes.

So we can take those 19 minutes and figure out what the .35 is in seconds. .35 x 60 = 21 seconds.

Now we have 19 minutes and 21 seconds

Repeat until you get desired units.

  • 0

Ah, thanks! I got it figured out now thanks :)

<?php
$seconds = 116101;

$days = $seconds / 86400;
$day_explode = explode(".", $days);
$day = $day_explode[0];

$hours = '.'.$day_explode[1].'';
$hour = $hours * 24;
$hourr = explode(".", $hour);
$hourrs = $hourr[0];

$minute = '.'.$hourr[1].'';
$minutes = $minute * 60;
$minute = explode(".", $minutes);
$minuter = $minute[0];

$seconds = '.'.$minute[1].'';
$second = $seconds * 60;
$second = round($second);
echo $day.' Days '.$hourrs.' Hours '.$minuter.' minutes, '.$second.' seconds';
?>

  • 0

Just so you know it doesn't seem to like single digit numbers.

1 seconds = 1 Days 3 Hours 0 minutes, 0 seconds

2 seconds = 2 Days 7 Hours 0 minutes, 0 seconds

3 seconds = 3 Days 0 Hours 0 minutes, 0 seconds

4 seconds = 4 Days 0 Hours 0 minutes, 1 seconds

5 seconds = 5 Days 0 Hours 0 minutes, 1 seconds

6 seconds = 6 Days 0 Hours 0 minutes, 1 seconds

7 seconds = 8 Days 2 Hours 0 minutes, 0 seconds

8 seconds = 9 Days 6 Hours 0 minutes, 0 seconds

0 seconds = splosion

Edited by omganinja
  • 0

Well...I fixed it...I think lol

Just encase anyone should need it sometime in the future

<?php
$seconds = 86401;

if ($seconds > 3600){
$days = $seconds / 86400;
$day_explode = explode(".", $days);
$day = $day_explode[0];
}else{
$day = 0;
}

if ($seconds > 3600){
$hours = '.'.@$day_explode[1].'';
$hour = $hours * 24;
$hourr = explode(".", $hour);
$hourrs = $hourr[0];
}else{
$hours = $seconds / 3600;
$hourr = explode(".", $hours);
$hourrs = $hourr[0];
}

$minute = '.'.@$hourr[1].'';
$minutes = $minute * 60;
$minute = explode(".", $minutes);
$minuter = $minute[0];



$seconds = '.'.@$minute[1].'';
$second = $seconds * 60;
$second = round($second);


echo @$day.' Days '.@$hourrs.' Hours '.@$minuter.' minutes, '.@$second.' seconds';
?>

  • 0

// Credits to Neil Wattam... | IF USED
$inputval = '3661'; // USER DEFINES NUMBER OF SECONDS FOR WORKING OUT | 3661 = 1HOUR 1MIN 1SEC


$unith =3600;		// Num of seconds in an Hour...
$unitm =60;			// Num of seconds in a min...

$hh = intval($inputval / $unith);	// '/' given value by num sec in hour... output = HOURS
$ss_remaining = ($inputval - ($hh * 3600));		// '*' number of hours by seconds, then '-' from given value... output = REMAINING seconds

$mm = intval($ss_remaining / $unitm);	// take remaining sec and devide by sec in a min... output = MINS
$ss = ($ss_remaining - ($mm * 60));		// '*' number of mins by seconds, then '-' from remaining sec... output = REMAINING seconds.

echo "ss: " . $ss . "<br>";
echo "mm: " . $mm . "<br>";
echo "hh: " . $hh . "<br>";

Credit goes to n_wattam @ phpbuilders

  • 0

Banjo - Well, that is one reason I asked on Neowin. I wasn't sure on the best way, but also wanted to try and do it myself. I only really know the basics of php. Working on a project to challenge me so I can learn more. :)

omganinja - Thanks. I'll have to adapt it to include days, but hopefully it wont be too much work. Math isn't my strong point. However I think I see how they did it. Thank you very much :)

Edit: Wow that was much easier...I added in days, a lot easier than I did with my script...thanks again :)

Edited by ncc50446
  • 0
$time=123456790; //whatever
$seconds = $time%60;
$mins = floor($time/60)%60;
$hours = floor($time/60/60)%24;
$days = floor($time/60/60/24);

Short and sweet :)

Thanks :)

I was reading the topic and it deceived me that nobody used the floor / ceiling / round functions of PHP for this.

This is the most optimized way of writing this code. (in terms of lines I mean, I have no idea if it's faster than anything else though, but I bet it is)

  • 0
Thanks :)

I was reading the topic and it deceived me that nobody used the floor / ceiling / round functions of PHP for this.

This is the most optimized way of writing this code. (in terms of lines I mean, I have no idea if it's faster than anything else though, but I bet it is)

The only way to optimise it further would combine the divisions into a single one for each calculation, but I prefer to leave it like for the added clarity.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Yes, it was amusing at the time because even then dbrand was well known for stealing the designs of products from other companies. That’s what they do.
    • Didn’t Dbrand once complain that Casetify was ripping off their designs a well? seems pretty bad of them to try and get around Valve’s copyright this way with that in mind.
    • Dbrand thought they could get away with this Steam Machine case, Valve disagreed by David Uzondu Image via Dbrand Dbrand has cancelled its highly anticipated Companion Cube enclosure for the Valve Steam Machine, which it teased back in November of last year with a concept render and sign-up page, because it did not ask Valve for permission first before manufacturing the case. According to Dbrand, it took the "backwards approach" of building the product first before asking for permission from the copyright holder. Seven months of work went into the project, requiring over a thousand engineering hours from the design team. Workers developed forty-four sets of injection molding tools, making a unique mold for each sub-component of the crate. When the Companion Cube went live on Monday last week, it, according to Dbrand, quickly became the second-fastest-selling product in the company's fifteen-year history, racking up orders for hundreds of thousands of units. Customers eagerly bought the $129.95 deluxe edition or the bare-bones $99.95 version, which the manufacturer cheekily branded as the "Poverty Cube". It was around this time that the legal eagles at Valve descended on the accessory maker with a formal demand. The developer pointed out that the iconic block design remains protected intellectual property from the game Portal, so unlicensed sales had to stop. Dbrand said that all its pleas to salvage the project with the Valve team, including proposals to run a properly licensed release under official terms "with their blessing", fell on deaf ears, so it had no choice but to obey and remove every trace of the product from the internet. If you bought the enclosure, the company said that banks will process your refund by the end of this week, but if it still hasn't arrived in your account by then, you should not hesitate to contact support. The Steam Machine itself is a high-performance console that Valve designed directly to bring PC gaming into the living room. It was announced on 12th November 2025 (the same day Dbrand announced the Cube) and runs on the Linux-based SteamOS, the same OS that powers the Steam Deck. As for the price, due to the shortage of memory and storage chips, the hardware cost landed much higher than people were expecting, starting at $1,049 for the 512 model (without a controller) or $1,128 with the new gamepad. The premium 2 TB model pushes those prices even higher, selling at $1,349 for the standalone console and hitting $1,428 if you want the bundle.
  • Recent Achievements

    • Rookie
      Almohandis went up a rank
      Rookie
    • Apprentice
      jahara21 went up a rank
      Apprentice
    • Reacting Well
      NovaEdgeX earned a badge
      Reacting Well
    • Week One Done
      NovaEdgeX earned a badge
      Week One Done
    • One Year In
      BA the Curmudgeon earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      534
    2. 2
      +Edouard
      266
    3. 3
      PsYcHoKiLLa
      148
    4. 4
      Steven P.
      97
    5. 5
      macoman
      57
  • Tell a friend

    Love Neowin? Tell a friend!