Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Compare Date

by rob_au (Abbot)
on Mar 05, 2002 at 12:37 UTC ( #149345=note: print w/replies, xml ) Need Help??

in reply to Compare Date

I will have a date in the database, and a cron script which runs nightly to check if there is one due to be run, like if a signup was the 5th - it will run the 5th of the next month.


There is a major flaw with this application logic - What if the signup was performed on the 31st of a month? Using this approach to this problem, the emails would be sent out to the user seven times over the year, that is, only those months with 31 days. In short - it doesn't work.

When faced with this application requirement previously, I approached this problem somewhat differently. The approach taken was to have the script check the number of days since the sign up was performed and send out the email if thirty days have passed - Or more specifically, the number of days within the year, divided by the number of emails which you wish to send to the user over the year.

To implement this application logic, the Date::Calc module proved to offer all of the date-related functions required - A snippet of the code employed follows:

use Date::Calc qw/:all/; . . my @date = (Localtime)[0..2]; . . # iterate through system accounts from the database while (my $account = $accounts->fetchrow_hashref) { my $plan = $plans[ $account->{'planid'} ]; # calculate the delta day count between the current date and the + # date of account activation my $delta = Delta_Days((split '-', $account->{'activationdate'}), +@date); if ($delta == 0) { # the delta day count will equal zero on the same day as the + # account activation - this is useful for when you also want + to # incorporate the sending of a welcome email or alike } else { # within this system each billing plans has a period value w +hich # represents the number of billing systems over a single ann +ual # period - if you are wanting to send out these emails on a # monthly basis, this variable can be replaced with the numb +er # twelve if ($plan->{'fee'} && $plan->{'period'}) { # here is the actual calculation itself, the modulus res +ult # of which will equal zero on days when an email should +be # sent to the user my $calc = $delta % int (Days_in_Year((@date)[0..1]) / $pl +an->{'period'}); if ($calc == 0) { # send the email to the owner of this account } } } }

For this project, I did also look at other date-related modules on CPAN but found the scope and module documentation of Date::Calc by far the most complete and inviting to work with.


perl -e 's&&[@.]/&&s&.com.&_&&&print'

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://149345]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2023-12-04 12:34 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (25 votes). Check out past polls.