Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Subtract two timestamps

by sandy1028 (Sexton)
on May 07, 2009 at 08:50 UTC ( [id://762522] : perlquestion . print w/replies, xml ) Need Help??

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

How to calculate the difference of two timestamps and display output in hrs, ..... minutes and .....Secs

Replies are listed 'Best First'.
Re: Subtract two timestamps
by gulden (Monk) on May 07, 2009 at 09:35 UTC
    use Date::Format; print &time2str("%H:%M:%S", ($time2 - $time1));
Re: Subtract two timestamps
by targetsmart (Curate) on May 07, 2009 at 09:32 UTC
    Date::Calc

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Subtract two timestamps
by Anonymous Monk on May 07, 2009 at 09:05 UTC
Re: Subtract two timestamps
by lakshmananindia (Chaplain) on May 07, 2009 at 08:53 UTC

    What output you need to show. Whether the difference b/w two time or after subtraction?

    Moreover what you have tried so for? so that we can provide guidelines to you.

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      I tried $start_time = time(); $diff = $end - $start; Some values in seconds I have got. I want the output in Number of Min,Number of secs

        There are 60 seconds in a minute and 60 minutes in an hour. A quick glance at Multiplicative Operators and a little ingenuity should see you through.

        Cheers,

        JohnGG

Re: Subtract two timestamps
by shmem (Chancellor) on May 07, 2009 at 10:40 UTC

    TIMTOWTDY... ;-)

    # file Secs.pm package Secs; use base Math::BigInt; use overload '""' => \&as_string, fallback => 1; sub as_string { my $h = int $_[0]/3600; my $m = int(($_[0]-$h*3600)/60); sprintf "%d:%02d:%02d",$h,$m,$_[0]-$h*3600-$m*60; } 1;
    use Secs; print Secs->new($end - $start),"\n";
Re: Subtract two timestamps
by bichonfrise74 (Vicar) on May 07, 2009 at 16:54 UTC
    Try this..
    #!/usr/bin/perl use strict; use Date::Manip; my $time_a = "12:00:00"; my $time_b = "11:10:00"; my $delta = DateCalc( $time_a, $time_b ); print join ":", ( (split( ":", $delta ))[4, 5, 6] );