• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Changing seconds into days, hours, minutes, seconds

Question

ncc50446    73

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 :)

Share this post


Link to post
Share on other sites

16 answers to this question

Recommended Posts

  • 0
Piggy    199

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.

Share this post


Link to post
Share on other sites
  • 0
astralbaby    8

:punk:

Share this post


Link to post
Share on other sites
  • 0
ncc50446    73

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';
?>

Share this post


Link to post
Share on other sites
  • 0
omganinja    26

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

Share this post


Link to post
Share on other sites
  • 0
ncc50446    73

It would seem my script has many more bugs than I first thought...

Share this post


Link to post
Share on other sites
  • 0
ncc50446    73

Its because of exponents it looks like.

1 / 86400 = 1.1574074074074074074074e-5

Guess I'll just have to add a few lines to check if its bigger than a certain number...too tired now to figure that all out though lol

Share this post


Link to post
Share on other sites
  • 0
ncc50446    73

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';
?>

Share this post


Link to post
Share on other sites
  • 0
Banjo    0

That's an incredibly inefficient and inaccurate way to do it. Look into the modulus operator and integer division, there are a ton of scripts that do this. You should never have to use string functions when doing something that's purely math

Share this post


Link to post
Share on other sites
  • 0
omganinja    26

// 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

Share this post


Link to post
Share on other sites
  • 0
ncc50446    73

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

Share this post


Link to post
Share on other sites
  • 0
DaveLegg    1,014

$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 :)

Share this post


Link to post
Share on other sites
  • 0
PyX    140
$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)

Share this post


Link to post
Share on other sites
  • 0
DaveLegg    1,014
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.

Share this post


Link to post
Share on other sites
  • 0
+SOOPRcow    133
$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 :)

I cant believe it took so many posts for someone to use the modulus operator.

Share this post


Link to post
Share on other sites
  • 0
Lt-DavidW    2

Edit: Oops, never mind. :)

Share this post


Link to post
Share on other sites
  • 0
ncc50446    73

Wow, that one is really simple! Thanks! :)

$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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.