Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Convert US dates to mySQL dates

by filmo (Scribe)
on May 01, 2001 at 04:04 UTC ( [id://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"; }

    HTH,

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

    EEjack

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://76825]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (1)
As of 2024-04-24 14:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found