Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

timestamp in perl

by baperl (Sexton)
on Sep 24, 2011 at 17:04 UTC ( #927664=perlquestion: print w/replies, xml ) Need Help??

baperl has asked for the wisdom of the Perl Monks concerning the following question:

hi, I am trying to insert a date (timestamp variable in mySQL) into mySQL table. the date is entered at the commandline so it is ARGV[0] in my Perl code. the issue is when I enter 2011-09-22 12:30:01 at the commandline, the sql table shows 2011-09-22 00:00:00. here's the relevant part of the code:
my $t_dt = $ARGV [0]; my $t_price = $ARGV [1]; my $ins_r = "INSERT INTO $tbl_name (t_dt, t_price) VALUES (?,?);"; $sth=$dbh->prepare($ins_r) or die $dbh->errstr;
so, for testing, I replaced $t_dt with the below, and it shows the current date and time in the table as it should, but since I am dealing with past dates, I can't use the below.
my $t_dt=strftime('%Y-%m-%d %H:%M:%S',localtime);
how can I format my $t_dt so when I enter a previous date(and time) at the commandline it gets inserted correctly into mySQL. thx!

Replies are listed 'Best First'.
Re: timestamp in perl
by Anonymous Monk on Sep 24, 2011 at 17:18 UTC

    That is impossible, either both should work, or neither one should

    You say that the strftime one works, that must mean you're giving the wrong variable to $sth->execute

      well, what I said is that when I enter at the commandline 2011-09-19 12:30:01 (i.e. not localtime), then in the table it gets inserted as 2011-09-19 00:00:00 instead of 2011-09-19 12:30:01 which I want it to insert. of course strftime works, but that only gives me current date and time...I want to enter past date and time. hope that clarifies
        ... when I enter 2011-09-22 12:30:01 at the commandline ...

        You do not show in the OP exactly what you are entering at the command line, and the Devil is in the details.

        If you enter something like
            perl 2011-09-22 12:34:56 19.95
        then  $ARGV[0] will be '2011-09-22' and  $ARGV[1] will be '12:34:56' (and  $ARGV[2] will be '19.95' – the price?).

        Try printing exactly what  $ARGV[0] is:
            print qq{'$ARGV[0]'};
        or maybe quoting the date-and-time on the command line: '2011-09-22 12:34:56' (of course, use double-quotes for Windoze).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927664]
Approved by Perlbotics
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-07-10 03:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found