Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Convert US dates to mySQL dates

by Daddio (Chaplain)
on May 01, 2001 at 01:07 UTC ( [id://76849]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Convert US dates to mySQL dates

Well, I don't know exactly what you are looking to do to "validate date", but the conversion is pretty straight forward:

my $date = "12-02-2000"; (my $out = $date) =~ s/(\d{1,2})-(\d{1,2})-(\d{2,4})/$3\-$1\-$2/;

Printing $out returns 2000-12-02. Sure, you can shorten it some, or use different syntax, but this should handle most of these date conversions.

Of course, the standard "use strict" and "-w" recommendations apply.

Replies are listed 'Best First'.
Re: Re: Convert US dates to mySQL dates
by diarmuid (Beadle) on May 01, 2001 at 11:44 UTC
    Well I would do it like the above and I guess the validation could be included also.
    $date="24-02-2000"; if ($date =~ /(\d{2})-(\d{2})-(\d{4})/){ $month=$1;$day=$2,$year=$3; print "ERROR month invalid",$month="01","\n" if($1 > 12); print "ERROR day invalid",$day="01","\n" if($2 > 31); print "ERROR year invalid",$year="2000","\n" if($3 > 2100 || $3 < 19 +00); $new_date = $year."-".$month."-".$day; } print "$new_date\n";
    You could be as fancy with the validation as you want.
    tidy up the code as much as you desire

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://76849]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.