> From: John [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, 3 March 2004 8:55 PM
> To: Perl Beginners
> Subject: Reformatting the Date

Hi John!

> 
> I receive the date from the Oracle as DD/MM/YY and i want to 
> insert that date in a mysql date field.

If you get a date in var '$odate', you need to convert it to an
intermediate format to handle
y2k issues, then convert it to text for insertion into mysql.

Eg

use POSIX qw( strftime mktime );

my $mysql_date;

if( $odate =~ m!^(..)/(..)/(,,)$! ) {
        $timestamp = mktime( 0,0,0,$1,$2-1,($3>40)?1900+$3:2000+$3 );
        $mysql_date = strftime( "%Y-%m-%d", localtime( $timestamp ) );
}

You could skip the 'mktime' and 'strftime' calls by just performing
string manipulation

if( $odate =~ m!^(..)/(..)/(..)$! ) {
        ($dd,$mm,$yy)=($1,$2,$3);
        $yyyy = ($yy>40) ? 1900+$yy : 2000+$yy;
        $mysql_date=sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
}

> 
> I know that mysql date form is YYYY-MM-DD.
> 
> Well, do you know if there is any date function to do the job?
> 
> Thanks in advance.
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to