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

Hi Monks!
I have this code:
#!/usr/bin/perl -w use strict; use Date::Calc qw( Today Today_and_Now Date_to_Days Add_Delta_Days ); use CGI qw(:standard); my $datetime = sprintf("%4d-%02d-%02d %02d:%02d:%02d.000",Today_and_No +w()); # prints on this format : 2011-06-08 21:26:23.000

Its the correct date time format but when it gets inserted into my sql database I am getting the time wrong, it is always 2 hours earlier, I don't understand why. Does anyone had an issue like that before?
Thanks for looking!

Replies are listed 'Best First'.
Re: Wrong time issue help!
by wind (Priest) on Jun 09, 2011 at 01:41 UTC
      Here simple as this, MySql Database, the field "date_in" is set as type datetime.
      ... my $datetime = sprintf("%4d-%02d-%02d %02d:%02d:%02d.000",Today_and_No +w()); ... my $sth = $dbh->prepare("INSERT INTO mytable(name,date_in) values(?,?) +") ,$dbh->errmsg); $sth->execute($name,$datetime); ...
      Thanks
Re: Wrong time issue help!
by ww (Archbishop) on Jun 09, 2011 at 02:26 UTC
    In addition to wind's questions, this, based on a WAG:

    Where is your database? Could it be on a server farm that's two hours west of your location?

      Yes that could be the issue, now is how to solve it!
        If (Note that that's a big "if"), in fact, the issue is one of timezones, possible fixes range from a simple minded, hard-coded addition (or subtraction, as the case may be) to or from your localtime... to converting all your date and time functions to use GMT.

        But "could be the issue" is a really poor foundation upon which to build a fix. First, determine if that "issue" is, indeed, the problem.