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!

date/time difference

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
I have a start date/time and end date/time in the following format:

yyyy-mm-dd hh:mi:ss

Is there any functions or anything to get the amount of second from the first date/time to the second date/time?
 
How about something like:

Code:
use Time::Local;

sub getSecs {
  my ($rawtime1, $rawtime2) = @_;
  my ($rawyr1, $rawyr2, $rawmon1, $rawmon2, $rawday1, $rawday2, $rawhr1, $rawhr2, $rawmin1, $rawmin2, $rawsec1, $rawsec2, $epoch1, $epoch2);

  ($rawcal1, $rawclock1) = split(/ /, $rawtime1);
  ($rawcal2, $rawclock2) = split(/ /, $rawtime2);

  ($rawyr1, $rawmon1, $rawday1) = split(/-/, $rawcal1);
  ($rawyr2, $rawmon2, $rawday2) = split(/-/, $rawcal2);
  ($rawhr1, $rawmin1, $rawsec1) = split(/:/, $rawclock1);
  ($rawhr2, $rawmin2, $rawsec2) = split(/:/, $rawclock2);

  $epoch1 = timelocal($rawsec1, $rawmin1, $rawhr1, $rawday1, $rawmon1-1, $rawyr1-1900);
  $epoch2 = timelocal($rawsec2, $rawmin2, $rawhr2, $rawday2, $rawmon2-1, $rawyr2-1900);

  return($epoch2 - $epoch1);
}

you'd use it like:

$secs = getSecs("2002-9-1 1:20:12", "2002-9-1 1:20:20");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top