Solved

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

18 Experts available now in Live!

Get 1:1 Help Now