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

In reply to Re: How do you format dates for entry into MySql? by lo_tech
in thread How do you format dates for entry into MySql? by glamring

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.