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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

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

    • No registered users viewing this page.