 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

March 5th, 2007, 01:05 PM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 5th, 2007, 05:33 PM
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|

March 9th, 2007, 11:48 AM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 10th, 2007, 07:46 AM
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|

March 12th, 2007, 12:06 PM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 12th, 2007, 12:08 PM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

March 15th, 2007, 05:43 AM
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
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
|

March 15th, 2007, 10:52 AM
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

April 11th, 2007, 03:44 PM
|
Registered User
|
|
Join Date: Apr 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |