http://qs321.pair.com?node_id=109583


in reply to How do you format dates for entry into MySql?

Here is a solution that might work for you. It makes use of the Date Calc module available on CPAN.

You can also brush up by reading the review

The following code will perform the date transmogrfication you need. It assumes that you really do want the days to be '00' and that you send the dates in the format you mentioned in your post.

I'm paranoid by nature and would assume nothing. I'd check for a valid date, probably using check_date($year,$month,$day) before returning the value form the sub.

Since this is a matter of neurosis, leave me to mine and I wont mention yours. ^.^

#!/usr/bin/perl -w use strict; use Date::Calc qw(Month_to_Text Decode_Month); sub mangle_dates { my $date = shift; if ( $date =~ /-/ ) { # we received a date with '-' in it, assume "YYYY-MM-DD" format my ($year,$month,$day) = split (/-/,$date); my $string = sprintf("%s %04d", Month_to_Text($month), $year); print "'$date' converted to '$string'\n"; return $string; } elsif ($date =~ / /) { my ($month,$year) = split (/ /,$date); # we received a date with ' ' in it, assume "MMM YYYY" format my $string = sprintf("%04d-%02d-00", $year, Decode_Month($month), +); print "'$date' converted to '$string'\n"; return $string; } } &mangle_dates("1999-05-15"); &mangle_dates("March 1925");