p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   get date difference between two dates in perl (http://p2p.wrox.com/showthread.php?t=54926)

crmpicco March 5th, 2007 01:05 PM

get date difference between two dates in perl
 
Code:

print "$startday-$startmon-$nyear\n"; # 2-4-2007
print "$endday-$endmon-$nyear\n"; # 9-4-2007

i have two dates in the format above, is there anyway to get the difference between them? the dates will change as its dynamic, so just a classic subtraction won't work. any ideas? examples?

Picco

www.crmpicco.co.uk
www.ie7.com

ciderpunx March 5th, 2007 05:33 PM

I'd use the Date::Calc module from CPAN.http://search.cpan.org/dist/Date-Calc/Calc.pod

Code:

($y,$m,$d) = Delta_YMD($year1,$month1,$day1, $year2,$month2,$day2);
Cheers

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

crmpicco March 9th, 2007 11:48 AM

that probably does work great, but i don't have Date::Calc installed on the server i'm using and I don't have the authoristion to install it. can it be done another way? maybe using more standard Perl modules?

www.crmpicco.co.uk
www.ie7.com

ciderpunx March 10th, 2007 07:46 AM

ah ok,

Well, if they were the other way round you could just order them as strings - i.e. 2007-4-9 comes after 2007-4-2 in the alphabet. In the following code there is a subroutine to swap the field ordering (reverse_date()). We then use the block form of the sort function to string compare the two dates with their fields reversed.

Code:

#!/usr/bin/perl
use warnings;
use strict;

sub reverse_date($) {
  my $date_to_reverse = shift;
  my @swap = split /-/,$date_to_reverse;
  return join '-',reverse @swap;
}

my $first_date = "9-4-2007";
my $second_date = "2-4-2007";

my ($earlier,$later) = sort {reverse_date($a) cmp reverse_date($b)} $first_date, $second_date;
print "Earlier date: $earlier\nLater date: $later\n";

On my computer this does:

Code:

charlie@mogadon:~$ ./tmp.pl
Earlier date: 2-4-2007
Later date: 9-4-2007
charlie@mogadon:~$

HTH

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

crmpicco March 12th, 2007 12:06 PM

cheers for that Charlie, I was meaning something that would give me the difference between two dates. Say 10 March 2007 and 27 February 2007. Returning 11 days. Taking into consideration the change of month and stuff like that (31 days....28 days....all that nastyness!)

www.crmpicco.co.uk
www.ie7.com

crmpicco March 12th, 2007 12:08 PM

Code:

    Earlier date: 2-4-2007
    Later date: 9-4-2007



    It also returns that on my machine......is there any way to modify it to get the difference in days???


www.crmpicco.co.uk
www.ie7.com

ciderpunx March 15th, 2007 05:43 AM

Sorry mate, me being dozy. Sounds like what you're actually after is $date1-$date2=$difference_in_days.

You /could/ write something to do this but it'd be a big gnarly thing to write, normally I'd just say use CPAN, but it sounds like you can't. If the dates are in a database you may find that you can do the date maths there and save yourself some faff. Otherwise, take a look at the source of Date::Calc and see how they did it!

Good luck

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

ciderpunx March 15th, 2007 05:46 AM

Oh or try this:

http://www.unix.org.ua/orelly/perl/cookbook/ch03_06.htm

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

crmpicco March 15th, 2007 10:52 AM

Code:

#!/usr/bin/perl

use strict;
use Date::Calc;

my @picco = (1981, 6, 16);      # 16 Jun 1981
my @gill  = (1973, 1, 18);      # 18 Jan 1973
$difference = Delta_Days(@picco, @gill);
print "There were $difference days between Picco and Gill\n";

superb...

www.crmpicco.co.uk
www.ie7.com

hurperl April 11th, 2007 03:44 PM

Hi Charlie,

your last post was very helpful. Even along with that, i am looking to change the date format from 01/12/07 to 12-Jan-2007, can you please tell me how can i do that and by using which module.

Thanks,
Hur
Quote:

quote:Originally posted by ciderpunx
 Oh or try this:

http://www.unix.org.ua/orelly/perl/cookbook/ch03_06.htm

--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk


All times are GMT -4. The time now is 05:53 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.