Gossamer Forum
Home : General : Perl Programming :

Number of days difference...

(Page 1 of 2)
> >
Quote Reply
Number of days difference...
Can someone help me with a sub to give me the number of days difference between a date which is held in a field in a database (date_exp) in the format "2001-12-01" and today's date?

TIA!!!
Quote Reply
Re: [donm] Number of days difference... In reply to
Try:

Code:

my @low = split /-/, $date_from_mysql;
my @high = split /-/, $todays_date;

$date1 = days($low[0],$low[1],$low[2]);
$date2 = days($high[0],$high[1],$high[2]);

$difference = $date2 - $date1;


sub days {

my ($year, $month, $day) = @_;
my ($tmp);
my ($secs);

$tmp = $day - 32075
+ 1461 * ( $year + 4800 - ( 14 - $month ) / 12 )/4
+ 367 * ( $month - 2 + ( ( 14 - $month ) / 12 ) * 12 ) / 12
- 3 * ( ( $year + 4900 - ( 14 - $month ) / 12 ) / 100 ) / 4;

return $tmp

}

I tested that with two dates one year apart and got:

365.242499999702

Not bad eh? Laugh


Last edited by:

PaulW: Dec 2, 2001, 6:09 AM
Quote Reply
Re: [donm] Number of days difference... In reply to
Try the moudle Date::Simple ... very simple interface for calculations between two dates.

Code:
use Date::Simple ('date', 'today');

# Difference in days between two dates:
$diff = date('2001-08-27') - date('1977-10-05');

Or you can use a more round-about method:

Code:
use Time::Local;

my $this = "13/02/1982"; # my birthday
my $that = "19/09/1983"; # my sister's birthday

my $seconds_diff = date_to_sec($that) - date_to_sec($this);
my $days_diff = int($seconds_diff / 86400);

print "I am $days_diff days older than my sister.\n";

sub date_to_sec {
my ($mon, $day, $year) = map { s/^0+//; $_ } split '/', shift;
return timelocal(0,0,12, $day, $mon - 1, $year - 1900);
}

Hope this helps!


- wil
Quote Reply
Re: [Wil] Number of days difference... In reply to
Which is essentially identical to my example except you don't need a module :)


Last edited by:

PaulW: Dec 2, 2001, 7:22 AM
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Yes. That's why in perl we say TMTOWTDI.

I think the first example I provided is much neater though, and it returns the number of days rounded up, not like your 365.425777... example.

Cheers

- wil
Quote Reply
Re: [Wil] Number of days difference... In reply to
>>and it returns the number of days rounded up, not like your 365.425777... example.
<<

I guess you could think like that, although that means mine is more accurate Laugh
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Yeah, I suppose your example would be more accurate, and would mean that you could not only return the number of days, but the hour, minute and even second if needed?

- wil
Quote Reply
Re: [PaulW] Number of days difference... In reply to
I tried both examples without any luck on either. I can't tell if your code is actually getting the dates?

Todays date and the date from the database called "date_exp".

Once we get todays date subtract it from the date_exp to get the difference.

Once I get the difference between the dates - I want to use an "if" statement like the following:

if ($per_admin || $per_mem && $difference < 0) {
print qq| Expired !|;
}
elsif ($per_admin || $per_mem && $difference < 30) {
print qq| Your membership will expire in $difference days !|;
}


All help appreciated - I just can't seem to get this to work?

--------------------------
donm
Quote Reply
Re: [donm] Number of days difference... In reply to
The code I gave works. All you need to do is substitute the bolded vars with todays date and your date from mysql.
Quote Reply
Re: [PaulW] Number of days difference... In reply to
I printed out what $difference is to the screen and I get the following number "725736.8475" - definitely not a number between 0 and 30?

Last edited by:

donm: Dec 2, 2001, 12:27 PM
Quote Reply
Re: [donm] Number of days difference... In reply to
Hm?

Who said it has to be between 0 and 30?

What if your dates are:

1980-01-10
2001-01-10

It calculates in _days_ so that is a 21 year difference so it would be 21 * 365

Last edited by:

PaulW: Dec 2, 2001, 1:19 PM
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Because the dates that I tried were actually within 1 day of themselves:

2001-12-03
2001-12-02

Should have returned a "1" shouldn't it?


Quote Reply
Re: [donm] Number of days difference... In reply to
Works fine for me:

http://213.106.6.135/cgi-bin/test.cgi

I used:

Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";


my @low = split /-/, '2001-01-10';
my @high = split /-/, '2001-01-11';

$date1 = days( $low[0], $low[1], $low[2] );
$date2 = days( $high[0], $high[1], $high[2] );

$difference = $date2 - $date1;

print $difference;


sub days {

my ($year, $month, $day) = @_;
my ($tmp);
my ($secs);

$tmp = $day - 32075
+ 1461 * ( $year + 4800 - ( 14 - $month ) / 12 )/4
+ 367 * ( $month - 2 + ( ( 14 - $month ) / 12 ) * 12 ) / 12
- 3 * ( ( $year + 4900 - ( 14 - $month ) / 12 ) / 100 ) / 4;

return $tmp

}
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Ok - let me show you what I have. Maybe I have something typed wrong. I am getting the following number for two dates that are only 1 day apart
"-731217.485"

Here is what I have:

Code:
$todays_date = &get_date();

my @low = split /-/, $todays_date;
my @high = split /-/, "$rec{'date_exp'}";
$date1 = days( $low[0], $low[1], $low[2] );
$date2 = days( $high[0], $high[1], $high[2] );
$difference = $date2 - $date1;

sub days {
my ($year, $month, $day) = @_;
my ($tmp);
my ($secs);

$tmp = $day - 32075
+ 1461 * ( $year + 4800 - ( 14 - $month ) / 12 )/4
+ 367 * ( $month - 2 + ( ( 14 - $month ) / 12 ) * 12 ) / 12
- 3 * ( ( $year + 4900 - ( 14 - $month ) / 12 ) / 100 ) / 4;
return $tmp
}

--------------------------
donm

Quote Reply
Re: [donm] Number of days difference... In reply to
You have them the wrong way around :)

$high needs to be todays date.
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Even then - it just turns the same number into a positive number rather than a negative number.
"731217.485"

The dates are 2001-12-02 and 2001-12-03

---------------------------
donm

Quote Reply
Re: [donm] Number of days difference... In reply to
print $rec{date_exp} and $todays_date to double check their value.
Quote Reply
Re: [donm] Number of days difference... In reply to
Just try this - it's much simpler. And it returns rounded values, and not values to x amount of decimal places.

Code:
use Date::Simple ('date', 'today');

# Difference in days between two dates:
$diff = date('2001-08-27') - date('1977-10-05');


- wil
Quote Reply
Re: [Wil] Number of days difference... In reply to
>>Just try this - it's much simpler.<<

Really?

Gives me a 500 Tongue....hmm guess thats because I need to su to root, download the module, install and compile.

Guess it's not simpler after all.

However as I've already shown, the code _does_ work so that isn't the issue.

Last edited by:

PaulW: Dec 3, 2001, 3:39 AM
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Why do you have to su to root to download, compile, install a CPAN module? Not on my web host, anyway. They let you download, compile and isntall any module you want in your own directory.

Although, that module was already installed.

- wil
Quote Reply
Re: [Wil] Number of days difference... In reply to
>>Why do you have to su to root to download, compile, install a CPAN module? <<

Well because you tend to have to install the module in the perl directory tree which is owned by root as well as updating other files and docs all owned by root Crazy

Also the module isn't included with perl by default. If donm wants to go to the trouble of downloading it then that doesn't worry me.
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Hmm. That's funny. Modules are usually designed to be stand-alone, and can be installed in any directory you wish. Therefore, you can install it into your own home directory which should be owned by you, and a decent host should give you enough trust ;-) to give you enough privilidges to install them too.

- wil
Quote Reply
Re: [Wil] Number of days difference... In reply to
>>Modules are usually designed to be stand-alone, and can be installed in any directory you wish. <<

Well actually thats not the case which is why there is a big fat module called CPAN which downloads and installs modules into the root using make/make test/make install

As for allowing people access to install modules...you've got a lot to learn. Allowing them to be uploaded into their own directory as a standalone is fine, allowing access to do installs from telnet/ssh is plain stupid. Thats what dedicated servers are for.

Last edited by:

PaulW: Dec 3, 2001, 6:15 AM
Quote Reply
Re: [PaulW] Number of days difference... In reply to
Sorry, but I'd imagine that telnet/ssh access to any account - dedicated, shared virtual is vital these days.

I wouldn't touch such a service without this 'feature'.

- wil
Quote Reply
Re: [Wil] Number of days difference... In reply to
When did we shift onto whether people should have telnet or ssh accounts?....we were talking about allowing people access to install modules a few minutes ago Tongue

Well I guess you didn't know that telnet is also one of the most stupid "features" for a host to offer. Thats why a lot hosts have blocked their telnet ports or turned off their server.

Anyway I really don't care. My gran is dying in hospital and this stupid conversation is winding me up more.

Later.
> >