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

Difference in 2 dates

Status
Not open for further replies.

BenRussell

Programmer
Mar 12, 2001
243
US
I have 2 dates in YYYYMMDDHHmmSS format. I know that $time1 is LESS THAN $time2, but I need to know how I can figure out how many MINUTES there are until $time1 reaches $time2.

Is there a module or some mathematical way to do this?

- Ben
 
$time1 = "20031020185003";
$time2 = "20031020185903";

my ($year1, $mon1, $day1, $hour1, $min1, $sec1) = $time1 =~ /(....)(..)(..)(..)(..)(..)/;


my ($year2, $mon2, $day2, $hour2, $min2, $sec2) = $time2 =~ /(....)(..)(..)(..)(..)(..)/;

$deficit = ($min2 - $min1);

print "The difference is $deficit\n";
 
Date::Manip also can do this for you and give you all kinds of nice output.
 
Ben,

I would convert the dates to Perl (UNIX) dates; these are actually the number of seconds since.. (can't remember, don't care) and you can add and subtract them sensibly.

There are also some nice modules to do date manipulation on Date::Manip (as siberian pointed out) is particularly good.

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Oh I love this!

$time1 = "20031020185003";
$time2 = "20031020185903";

print "Difference of times: ". ($time2/100)-($time1/100) . "\n";

That one was too easy. Thank Perl for being so loose!
 
Doh! Sorry for my short-sightedness.

print "Difference of times: ". int($time2/100)-int($time1/100) . "\n";
 
Doh! Sorry for my short-sightedness.

If you need it be a round integer:

print "Difference of times: ". int($time2/100)-int($time1/100) . "\n";
 
soy4soy, I used your method, but when I compare the 2 dates (that are exactly one hour apart), it gives me the number 100, but it should be 60. How can I achieve thsi?

- Ben
 
$#@!

Sorry. Again, short-sighted me. Of course there are not 100 minutes in an hour, nor 100 hours in a day, nor 100 days in a month., etc...

The short answer is: Use siberian's or MikeLacey's answers.

The long answer is to do it yourself:

In short, you will want to convert the timestamps to "epoch seconds", do your math, and then divide your answer by 60 when done.

Use the timelocal() or timegm() functions in the standard Time::Local module.

My apologies for submitting an incorrect resolution!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top