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

Re: Date::Manip Help

by sacked (Hermit)
on May 10, 2004 at 15:12 UTC ( [id://352101]=note: print w/replies, xml ) Need Help??


in reply to Date::Manip Help

You need to change the format of your date to that used by MySQL (YYYY-MM-DD) before calling your INSERT statement. You can do this with Date::Manip::UnixDate:
$date= ParseDate("in 90 days"); $date= UnixDate( $date => '%Y-%m-%d' ); print "Date: $date\n"; # 2004-08-08 $rows=$dbh->do("INSERT INTO appCancel (appt_date,status, no_cancel, ci +ty) VALUES('$date' , 'U' , '0' , 'M')") || die "Couldn't insert record : $DBI::errstr";
See the section on UnixDate in the pod for Date::Manip.

--sacked

Replies are listed 'Best First'.
Re: Re: Date::Manip Help
by Agyeya (Hermit) on May 11, 2004 at 05:56 UTC
    I tried the following, but all the dates in the table are coming the same
    $date=ParseDate("today"); my @city=("C","K","D"); my ($j,$fut); $fut=ParseDate("in 90 days"); for ($j=0;$j<3;$j++) { my $i; for ($i=1;$i<90;$i++) { # $date=ParseDate("in 1 day"); $date= UnixDate( $date => '%Y-%m-%d' ); print "Date: $date\n"; # $rows=$dbh->do("INSERT INTO appCancel (appt_date,status, no_c +ancel, city) # VALUES ('$date','U','0', '$city[ +$j]')") || # die "Couldn't insert record : $DBI::errstr"; $date=ParseDate("in 1 day"); print "Date: $date\n"; } }
    the number of records being generated is 267? and not 270
      Please take a look at the documentation for Date::Manip, as I suggested in my earlier post. You need the DateCalc function to calculate ($somedate + 1 day).

      Regarding your code above, in your original post, it wasn't clear that you wanted to enter every date from tomorrow to 90 days from now. If that's the case, you need to create a new date object for each day:
      #!/usr/bin/perl # get current Date from MySql use warnings; use strict; use Date::Manip; + my @city=("C","K","D"); my $j; for ($j=0;$j<3;$j++) { my $date= ParseDate("today"); + my $i; + # either start from 0, or use <= for ($i=1;$i<=90;$i++) { $date= DateCalc($date, "+ 1 day"); $date= UnixDate($date, '%Y-%m-%d'); print "Date: $date, city: $city[$j]\n"; } }
      The reason you were only getting 267 records is because your inner loop only executed 89 times:
      for ($i=1;$i<90;$i++)
      You either need to initialize $i to 0 or change the conditional expression to $i <= 90.

      If you'd like to speed up the code, you should reverse the loop structure, with the date calculation in the outer loop (so each date is only calculated once rather than three times). Also note that the code below uses Perl-style loops rather than C-style:
      #!/usr/bin/perl # get current Date from MySql use warnings; use strict; use Date::Manip; + my @city=("C","K","D"); + my $date= ParseDate("today"); + # using a Perl-style (not C-style) loop helps prevent # the error seen in the earlier loop construct for my $i ( 1 .. 90 ) { $date= DateCalc($date, "+ 1 day"); $date= UnixDate($date, '%Y-%m-%d'); + for my $j ( 0 .. 2 ) { print "Date: $date, city: $city[$j]\n"; } }
      Hope this helps,

      --sacked
        Thanx
        this worked. I had trying to break my head over this for the past few days

        Regards
        Agyeya Gupta

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-18 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found