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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time difference problem

Status
Not open for further replies.

gm199

Programmer
Aug 9, 2001
37
US
Hi all, I made a script that give me the hour:minutes of the beginning of the event and the hour:minutes of the end.
Now I need to calculate the time difference between those times.
Ie:
Begins at 6:53
Ends at 19:02
Duration: ??:?? #(must show 12:09)

In this case I change it to minutes and divided by 60 but since minutes 02 is < then 53, i got a negative number.

Any clue on how to perform the math without any additional package?
 
Are you storing the times as the values you would get back from gmtime()? If you are you are storing the number of seconds since some time or other...

So -- you can take one time from the other and get the number of seconds

Divide to get days
divide remainder to get hours
=======||============== minutes
what's left is seconds
Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
No, the time is provided by a routine that caalculates the sunnrise and sunshine.
Here is the partial uglly code I'm getting in trouble:

#Sunrise = 6:53
#Sunset = 19:02

($hh,$mm) = split(/:/, $sunrise);
$in = (($hh*60)+$mm);

($ho,$mo) = split(/:/, $sunset);
$out = (($ho*60)+$mo);

$vv =(($out - $in)/60);

$dia = ($sunset - $sunrise);
($hs,$ms)=split(/\./,$vv);
$vf = ($vv-int($dia));
$vf =~ s/-//; # stupid, I know

$m2 = int($vf*60);

if ($m2 < 10){ $mif = &quot;0&quot;.$m2; }
else{ $mif=$m2; }

print &quot;Hours of sunshine &quot; .$hs . &quot;:&quot; . $mif . &quot;<br>&quot;;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top