Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert seconds to hours 1

Status
Not open for further replies.

JonnoGT

Technical User
Aug 20, 2002
96
GB
Hi, newbie question for you.

I've got a value in seconds - eg 34200
that I need to convert into hours. so that it reads 09:00:00

How would I do that?
 
did you mean 32400 not 34200?

kind of like this - I think!?:-

Code:
[b]#!/usr/bin/perl[/b]

($seconds, $minutes, $hour, $dayOfMonth, $month, $year, $dayOfWeek, $dayOfYear) = localtime(32400);

$hour--;

print "$hour:$minutes:$seconds\n";


Kind Regards
Duncan
 
Code:
[b]#!/usr/bin/perl[/b]

@seconds = qw(32400 45000 31400 3600 22000 5500);

foreach $timeInSeconds (@seconds) {

  ($seconds, $minutes, $hour, $dayOfMonth, $month, $year, $dayOfWeek, $dayOfYear) = localtime($timeInSeconds);

  $hour--;

  print "$hour:$minutes:$seconds\n";

}

outputs:-

Code:
[blue]9:0:0
12:30:0
8:43:20
1:0:0
6:15:0
1:31:40
[/blue]

you'll need sprintf to format correctly


Kind Regards
Duncan
 
jonnoGt,

I think this might be more what you were looking for:

Code:
$t = 32400
$h = int($t/3600);
$m = int(($t % %h)/60);
$s = $m % 60;
printf("%.2d:%.2d:%.2d\n", $h, $m , $s);

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Rod's solution is similar to the way I was going about solving this - his code just needs a couple corrections and it works great.

Code:
$t = 32400[red];[/red]
$h = int($t/3600);
$m = int(($t % [red]($h*3600)[/red])/60);
$s = [red]$t[/red] % 60;
printf("%.2d:%.2d:%.2d\n", $h, $m , $s);
 
I think Rod should was politely saying "Don't listen to that muppet!" (Duncan). If he didn't - he should have :)

Rod's solution is DEFINITELY the one to go with

What was I thinking!?


Kind Regards
Duncan
 
rharsh,

Thanks for the corrections. I'm not sure what I was thinking, either in the coding, or in using a test case that didn't have minutes or seconds.

Sheesh.



Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top