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

How to find the 12 hour time difference in time function $oldtime=time(); #lets say this value captured sevral hours ago $current_time=time(); what is the easiest way to find there time difference is greater than 12 hours. Thank you

Replies are listed 'Best First'.
Re: Time Difference
by duct_tape (Hermit) on Mar 17, 2005 at 22:59 UTC

    time() returns number of seconds since the epoch so to figure out if there is a difference of more than 12 hours all you have to do is check for the difference using seconds (12hours = 60*60*12).

    if ($current_time - $oldtime > 43200) { print "There is > 12 hour difference\n"; }

    If you need to do more complex date/time calculations you may benefit from the many date modules on CPAN.

Re: Time Difference
by tphyahoo (Vicar) on Mar 18, 2005 at 09:25 UTC
    FWIW, this works with time(), but not localtime().

    (With localtime, you get an "argument isn't numeric in subtraction" error.)

    #filename: stickOutputOnEnd.pl use warnings; use strict; my $time1 = time(); sleep(5); my $time2 = time(); my $timedifference = $time2-$time1; # doesn't work if you use localtim +e. print scalar $timedifference;

      FWIW, this works with time(), but not localtime().

      Er, yes. Because they have two quite distinct functions. time() returns the epoch seconds representing the current time and localtime() (or gmtime()) returns a list representing the parts of the time (like the struct tm returned by the similarly named C library functions.) when in list context or a string represention thereof when in scalar context.

      /J\

        Thank you all. That really helps.