Re: Comparing two dates and getting back difference in days
by Roy Johnson (Monsignor) on Jan 21, 2004 at 16:34 UTC
|
| [reply] |
Re: Comparing two dates and getting back difference in days
by jdtoronto (Prior) on Jan 21, 2004 at 16:35 UTC
|
Try Time::Piece. I discovered it a few days back and it is mucho-small, very fast and does the basic comparisons. It has relatives Time::Piece::MySQL for MySQL date/time types and when you do a calculation it returns a Time::Seconds object. Very nice little bundle.
I also use Date::Manip, but it is too big and heavy for this sort of lifting!
jdtoronto | [reply] |
Re: Comparing two dates and getting back difference in days
by Aragorn (Curate) on Jan 21, 2004 at 16:38 UTC
|
Date_Cmp just returns if $date1 is smaller than, equal to, or greater than $date2, like the Perl cmp operator.
What you want to do can be done with the DateCalc function. See the documentation of Date::Manip for details.
The Date::Calc module is implemented mostly in C and should be a lot faster.
Arjen | [reply] [d/l] [select] |
|
Ok, I'm looking at DateCalc but I can't seem to figure out how to get the output in just days. I'm getting output that looks like this:
2004-01-14 2004-01-21 +0:0:1:0:0:0:0
Do I have to do my own conversion to just days ? Also, I could seem to figure out what the $err was supposed to be. | [reply] [d/l] |
Re: Comparing two dates and getting back difference in days
by duff (Parson) on Jan 21, 2004 at 16:38 UTC
|
#!/usr/bin/perl
use Time::Local;
my $now_gmt = time;
my $then = "2004-01-14";
my ($ty,$tm,$td) = $then =~ /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
my $then_gmt = timegm(0,0,0,$td,$tm-1,$ty);
my $diff_in_days = int(($now_gmt - $then_gmt) / 86400);
| [reply] [d/l] |
Re: Comparing two dates and getting back difference in days
by Anonymous Monk on Jan 21, 2004 at 17:23 UTC
|
Date::Simple does a nice job for simple calculations.
Chris
#!/usr/bin/perl
use strict;
use warnings;
use Date::Simple;
my $date1 = Date::Simple->new("2004-01-14");
my $date2 = Date::Simple->new("2004-01-17");
print "Days difference is: ", $date2 - $date1, "\n";
| [reply] [d/l] |
Re: Comparing two dates and getting back difference in days
by Limbic~Region (Chancellor) on Jan 21, 2004 at 17:01 UTC
|
TASdvlper,
Here is a TIMTOWTDI solution. The Seconds2English module you will not find on CPAN as I wrote for learning purposes. I have actually refined it quite a bit, but unfortunately that code is unavailable at the moment.
#!/usr/bin/perl -w
use strict;
use Time::Local;
use Seconds2English;
my $date1 = epoch('2004-01-14');
my $date2 = epoch('2003-12-25');
my $diff = Seconds2English->new('start' => $date2 - $date1);
print "The difference is " , $diff->in_days, " days\n";
sub epoch {
my ($year, $month, $day) = split '-' , shift;
return timelocal(0 , 0 , 12, $day , $month - 1, $year);
}
__END__
The difference is 20 days
Cheers - L~R | [reply] [d/l] |
Re: Comparing two dates and getting back difference in days
by phydeauxarff (Priest) on Jan 21, 2004 at 17:37 UTC
|
Throwing in one more thing to consider when comparing dates...
I use date::business because I typically need to figure out how many "business days" have passed and this module does a good job of not only figuring the weekdays, but also allows you to skip holidays.
using the diffb() function in this module will count the number of days between a Friday and a Monday as 1. | [reply] |
Re: Comparing two dates and getting back difference in days
by BrowserUk (Patriarch) on Jan 21, 2004 at 18:23 UTC
|
| [reply] |
|
If you look at the rest of the nodes that I have written you will discover that I am a big Date::Manip fan.
That been said, I think that "slow" is a relative term. Yes it is big, but when ever I have implimented it into my scripts, I have found very little, if any, time lag. If you are trying to scrape every last clock cycle out of a huge script, you probably don't want to use it. But if a milli-second or so doesn't matter to you, the flexibility of Date::Manip makes it more than worth it. I can honestly say that I haven't used any of the other Date conversion modules, mainly because Date::Manip has everything that I need. I guess once you get to know a module, you kind of get stuck on it. Mind you that I am not knocking any of the other modules, I just think that for what you get out of it, Date::Manip is where it's at.
| [reply] |
Re: Comparing two dates and getting back difference in days
by flyingmoose (Priest) on Jan 21, 2004 at 17:24 UTC
|
So many Date and Time modules to try, so little time :)
| [reply] |
|
That's how it used to be before DateTime.
That's why there is only DateTime now.
use DateTime;
use strict;
use warnings;
my $str = "2004-01-14";
my( $y, $m, $d ) = split /\D/,$str;
my $delta = DateTime->new(
year => $y,
month => $m,
day => $d,
)->delta_days(
DateTime->now(),
);
die $delta->days;
__END__
0 at datetime4.pl line 16.
| [reply] [d/l] |
|
| [reply] |
|
Re: Comparing two dates and getting back difference in days
by injunjoel (Priest) on Jan 22, 2004 at 18:30 UTC
|
Greetings all,
A little late I know but here is what I use for small cases.
#!/usr/bin/perl -w
use strict;
use Time::Local;
my $start_date_str = "2004-01-14";#date you are testing;
my ($year,$month,$day) = split(/\-/,$start_date_str);
$month--;#timelocal months are 0-11;
my $start_seconds = timelocal(0,0,0,$day, $month, $year);
my $today_seconds = time();
my $days_between = ( int( ( ( (abs($start_seconds - $today_seconds))/6
+0 )/60 )/24 ) );
print $days_between;
exit;
Previewing it I must admit its not very elegant, but it works for me.
Hope that helps get you part of the way there.
| [reply] [d/l] |
|
This is great code, thanks!
| [reply] |