start page | rating of books | rating of authors | reviews | copyrights

Perl Cookbook

Perl CookbookSearch this book
Previous: 3.4. Adding to or Subtracting from a Date Chapter 3
Dates and Times
Next: 3.6. Day in a Week/Month/Year or Week Number
 

3.5. Difference of Two Dates

Problem

You need to find the number of days between two dates or times.

Solution

If your dates are in Epoch seconds, and fall in the range Fri Dec 13 20:45:52 1901 to Tue Jan 19 03:14:07 2038 (inclusive), simply subtract one from the other and convert the seconds to days.

$seconds = $recent - $earlier;

If you have distinct DMYMHS values, or are worried about the range limitations of Epoch seconds, use the Date::Calc module from CPAN. It can calculate the difference between dates:

use Date::Calc qw(Delta_Days); $days = Delta_Days( $year1, $month1, $day1, $year2, $month2, $day2);

It also calculates the difference between dates and times:

use Date::Calc qw(Delta_DHMS); ($days, $hours, $minutes, $seconds) =   Delta_DHMS( $year1, $month1, $day1, $hour1, $minute1, $seconds1,  # earlier               $year2, $month2, $day2, $hour2, $minute2, $seconds2); # later

Discussion

One problem with Epoch seconds is how to convert the large integers back to forms that people can read. The following example shows one way of converting an Epoch seconds value back to its component numbers of weeks, days, hours, minutes, and seconds:

$bree = 361535725;          # 16 Jun 1981, 4:35:25 $nat  =  96201950;          # 18 Jan 1973, 3:45:50  $difference = $bree - $nat; print "There were $difference seconds between Nat and Bree\n"; 



There were 265333775 seconds between Nat and Bree



  $seconds    =  $difference % 60; $difference = ($difference - $seconds) / 60; $minutes    =  $difference % 60; $difference = ($difference - $minutes) / 60; $hours      =  $difference % 24; $difference = ($difference - $hours)   / 24; $days       =  $difference % 7; $weeks      = ($difference - $days)    /  7;  print "($weeks weeks, $days days, $hours:$minutes:$seconds)\n"; 



(438 weeks, 4 days, 23:49:35)



Date::Calc's functions can ease these calculations. The Delta_Days function returns the number of days between two dates. It takes the two dates as a list: year, month, day. The dates are given chronologically  - earliest first.

use Date::Calc qw(Delta_Days); @bree = (1981, 6, 16);      # 16 Jun 1981 @nat  = (1973, 1, 18);      # 18 Jan 1973 $difference = Delta_Days(@nat, @bree); print "There were $difference days between Nat and Bree\n"; 



There were 3071 days between Nat and Bree



The Delta_DHMS function returns a four-element list corresponding to the number of days, hours, minutes, and seconds between the two dates you give it.

use Date::Calc qw(Delta_DHMS); @bree = (1981, 6, 16, 4, 35, 25);   # 16 Jun 1981, 4:35:25 @nat  = (1973, 1, 18, 3, 45, 50);   # 18 Jan 1973, 3:45:50 @diff = Delta_DHMS(@nat, @bree); print "Bree came $diff[0] days, $diff[1]:$diff[2]:$diff[3] after Nat\n"; 



Bree came 3071 days, 0:49:35 after Nat



See Also

The documentation for the CPAN module Date::Calc


Previous: 3.4. Adding to or Subtracting from a Date Perl Cookbook Next: 3.6. Day in a Week/Month/Year or Week Number
3.4. Adding to or Subtracting from a Date Book Index 3.6. Day in a Week/Month/Year or Week Number