Solved

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

Posted on 2004-04-08
7
203 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PERL export multiple query results to a JSON file 1 165
crawling gofundme 4 126
Bash one liner, start and end time calculation 13 109
Call Shell Script from Perl Script 6 97
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

805 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