Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Convert US dates to mySQL dates

by filmo (Scribe)
on May 01, 2001 at 04:04 UTC ( #76825=perlquestion: print w/replies, xml ) Need Help??
filmo has asked for the wisdom of the Perl Monks concerning the following question:

standard CGI input is text field in accepting MM-DD-YYYY formated dates. Want to (1) validate date (2) convert it to mySQL format YYYY-MM-DD. Don't want to have to use a seperate Month, Day, Year param.

Replies are listed 'Best First'.
Re: Convert US dates to mySQL dates
by sutch (Curate) on May 01, 2001 at 05:18 UTC
    I had a similar problem earlier today. I needed a method to parse human-readable dates that were generated by various systems. I found that the Time-modules, and specifically Time::ParseDate, should be able to convert various date formats to the time in seconds since January 1st, 1970 (the epoch).

    This solves the most difficult part of your problem. I'm not sure if there is a module that handles the conversion to MySQL format--in the past I've used localtime to do these types of conversions.

Re: Convert US dates to mySQL dates
by Daddio (Chaplain) on May 01, 2001 at 05:07 UTC

    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.

      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
Re: Convert US dates to mySQL dates
by eejack (Hermit) on May 01, 2001 at 05:11 UTC
    There has to be a better way to do this - but this should get you through till then.

    $test_date = "12-02-2001"; if ($test_date =~ s/^([0-1]?\d)\-([0-3]?\d)\-([0-9]{4})$/$3\-$1\-$2/) +{ print $test_date; } else { print "boom"; }


    Update thought about it, shortened it and corrected it. Sorry for the original error in it.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://76825]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-06-20 12:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.