Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Subtract two timestamps

by sandy1028 (Sexton)
on May 07, 2009 at 08:50 UTC ( #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

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



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

    TIMTOWTDY... ;-)

    # file 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] );

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://762522]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2023-09-27 08:43 GMT
Find Nodes?
    Voting Booth?

    No recent polls found