Nik has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I finally used the mysql timestamp datatype and it worked but is shows the string like this: 0406141832 which is (YYMMDDHHmm). its ok but in my webpage i cant just display the string like this. i need it to be like "MM DD, HH:mm". How can i format it to be like this? or is there any other way to get the time as i want to without have to converting it?
The Devil Is In The Details!

20040618 Edit by castaway: Changed title from 'Substitution operator question'

Replies are listed 'Best First'.
Re: Formatting a date
by Ven'Tatsu (Deacon) on Jun 14, 2004 at 16:05 UTC
    Not a perl solution but I would just have MySQL format it for you take a look at DATE_FORMAT(date,format) in the MySQL documentation. I belive this should do what your looking for:
    select DATE_FORMAT(timestamp, '%m %d, %H:%i') from ....
Re: Formatting a date
by amw1 (Friar) on Jun 14, 2004 at 16:19 UTC
    you can also do
    select UNIX_TIMESTAMP(timestamp_field) as timestamp_field ... execute query put timestamp_field into $timestamp_field print scalar(localtime($timestamp_field))
    This way everything that deals with seconds since epoch will work on the data you get out of your database. Not exactly the format you wanted but getting to that format should be pretty straight forward once you have time in a more standard format.
Re: Formatting a date
by fluxion (Monk) on Jun 14, 2004 at 16:27 UTC
    The above MySQL solution would be best, but if you insist on doing it with Perl you can just parse the string using a position-oriented regex like:
    $date_string = "0406141832"; ($year,$month,$day,$hour,$minute) = ( $date_string =~ /(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/ );
    From there you can manipulate it however you want, but to make it a little more robust you can create a DateTime object from the data so you can manipulate it a little more freely.

    Roses are red, violets are blue. All my base, are belong to you.
Re: Formatting a date
by kesterkester (Hermit) on Jun 14, 2004 at 16:20 UTC
    A Perl solution would be to use the match operator to match all two-character substrings, and then catch the matches, like so:

    $time = "0406141832"; ($y,$m,$d,$h,$min) = $time =~ /../g; print "$m $d, $h:$min"
Re: Formatting a date
by borisz (Canon) on Jun 14, 2004 at 16:13 UTC
    perl -e 'printf "%02d %02d, ("0406141832" =~ /(\d\d)(\d\d)(\d\d)(\d\d) +(\d\d)/)[1..4];'
    Boris
Re: Formatting a date
by edan (Curate) on Jun 15, 2004 at 08:24 UTC

    I would recommend the MySQL solution offered by Ven'Tatsu, since my philosophy is to do everything on the DB side that you can get away with. That, and you should make use of the handy functions provided by the MySQL folks, since it gives them warm-fuzzy-feelings when you do.

    Since this problem could come up in another context where you would need a perl solution, and everybody else who answered so far used regexen, which I think is unneccesary here, I offer the following:

    #!/usr/bin/perl use strict; use warnings; my $date = qq(0406141832); my ($YY, $MM, $DD, $hh, $mm) = unpack("A2 A2 A2 A2 A2", $date); my $formatted = "$MM $DD, $hh:$mm"; print $formatted, $/;

    unpack is a nice, straightforward, and fast solution to parsing fixed-width strings like this. Just something to add to your bag-o-tricks.

    --
    edan