Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do you format dates for entry into MySql?

by lo_tech (Scribe)
on Sep 01, 2001 at 04:41 UTC ( #109583=note: print w/replies, xml ) Need Help??


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");

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://109583]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2021-03-03 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (69 votes). Check out past polls.

    Notices?