Solved

how to use a date module within a cron to send out 2 day, 7day, 30day messages?

Posted on 2004-04-08
7
196 Views
Last Modified: 2010-03-04
Hi,

I'm writing a script that sends out remindermessages 2days, 7days, and 30days after a customer signs up with my services.

I currently log all orders in a log file...  I'm able to retrieve the date/email from the log file....  and I'm able to send people email messgaes just fine in my script.  The problem is that I have no idea how to use a date function to determine which customers need to be sent a message.  I assume it is probably like 5 lines of code if any one here is familiar with a date module.

So for example:  

Today is April 1st.  Therefore I run the cron at 1am on April 1st.    I have a client that signed up on March 30th.  (2 days ago).  How do I go about determining, that this customer needs to be sent an email?  The variable I use for the day the client signed up is $signupdate .   $signupdate is in this format:  4-1-2004

Any help would be much appreciated.  

Regards,

Wyatt
0
Comment
Question by:wyatt12
  • 3
7 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 10788483
use Date::Manip;

http://www.perldoc.com/perl5.6.1/lib/Date/Manip.html

When I have a few extra minutes, I'll post some example code that you can use.
0
 
LVL 10

Expert Comment

by:rj2
ID: 10789627
use Date::Calc qw(Add_Delta_Days);
$signupdate='4-1-2004';

if($signupdate=~m/(\d+)-(\d+)-(\d{4})/) {
      ($ysignup,$msignup,$dsignup)=($3,$1,$2);
      ($y2, $m2, $d2) = Add_Delta_Days($ysignup, $msignup, $dsignup, 2);
      ($y7, $m7, $d7) = Add_Delta_Days($ysignup, $msignup, $dsignup, 7);
      ($y30, $m30, $d30) = Add_Delta_Days($ysignup, $msignup, $dsignup, 30);
      
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);      
        $today=sprintf("%d-%d-%d",$mon+1,$mday,$year+1900);
        $date2=sprintf("%d-%d-%d",$m2,$d2,$y2);
        $date7=sprintf("%d-%d-%d",$m7,$d7,$y7);
        $date30=sprintf("%d-%d-%d",$m30,$d30,$y30);
       
        #print "$today,$date2,$date7,$date30\n";
       
        if($today eq $date2 || $today eq $date7 || $today eq $date30) {
              print "We should send reminder today\n";
        } else {
              print "We should not send reminder today\n";
        }
}
0
 
LVL 8

Accepted Solution

by:
davorg earned 250 total points
ID: 10789782
You can do it all without having to resort to installing new modules from CPAN.

use Time::Local;

use constant DAY => 24  * 60 * 60;
use constant TWO_DAYS = 2 * DAY;
use constant SEVEN_DAYS => 7 * DAY;
use constant THIRTY_DAYS => 30 * DAY;

my $signupdate = '4-1-2004'; # 4th Jan 2004 in any sensible country :)

my ($d, $m, $y) = split /-/, $signupdate;

my $signup_secs = timelocal(0, 0, 0, $d, $m - 1, $y - 1900);
my $now = time;

my $diff = $now - $signup_secs;
if ($diff > TWO_DAYS and $diff < (TWO_DAYS + DAY)) {
  # send two day reminder
}

if ($diff > SEVEN_DAYS and $diff < (SEVEN_DAYS + DAY)) {
  # send seven day reminder
}

if ($diff > THIRTY_DAYS and $diff < (THIRTY_DAYS + DAY)) {
  # send thirty day reminder
}

0
 
LVL 10

Assisted Solution

by:rj2
rj2 earned 250 total points
ID: 10789809
Or somewhat shorter like this

use Date::Calc qw(Delta_Days);
$signupdate='4-1-2004';

if($signupdate=~m/(\d+)-(\d+)-(\d{4})/) {
      ($ysignup,$msignup,$dsignup)=($3,$1,$2);      
      ($mday, $mon, $year) = (localtime)[3,4,5];
      $diff=Delta_Days($ysignup,$msignup,$dsignup,
                       $year+1900,$mon+1,$mday);
       
        #print "$diff\n";
        if($diff==2 || $diff==7 || $diff==30) {
              print "We should send reminder today\n";
        } else {
              print "We should not send reminder today\n";
        }
}
0
 
LVL 10

Expert Comment

by:rj2
ID: 10789826
Run command below to install module Date::Calc if not already installed

perl -MCPAN -e 'install Date::Calc'

Date::Calc is a nice module because it can do all the most common date calculations and is also much faster that Date::Manip.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 90
PHP equivalent of opening a com object 5 69
Perl script not writing to .csv file 6 106
PERL variable conundrum 9 89
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now