Perl Monk, Perl Meditation PerlMonks

### comment on

 Need Help??
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&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

In reply to Re: Compare Date by rob_au
in thread Compare Date by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2023-12-04 04:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What's your preferred 'use VERSION' for new CPAN modules in 2023?

Results (21 votes). Check out past polls.

Notices?