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

hello,

did some several searches on this board on how to convert a mysql timestamp into a scalar that you can print out in html thats formatted.

i have a mysql column that automatically updates the date and time using a timestamp column.

it looks like this.
20050519112656

How can I convert that string into a well formatted Date? I'm thinking something like 05-19-2005 4;00 amest --or something?? I have no clue where to begin, some of the threads I read had a a few ways (posix, timestamp module???) --but I'm just not sure on which one is right for the situation I'm in?

-AM

Replies are listed 'Best First'.
Re: formatting mysql timestamp
by jhourcle (Prior) on Jun 19, 2005 at 10:59 UTC

    If the data is still in mysql, I'd have mysql do the work, and use the DATE_FORMAT() function.

    In more recent versions of mySQL, there's also the CONVERT_TZ() function (but you'll need to load the time zone tables)

    The following should work in most versions of mysql:

    date_format date_add('20050519112656',interval 4 hour), '%m-%d-%y %h:%i %p EST')

    (I'd also personally suggest not using m/d/y ordering ... it might make sense to americans, but can cause lots of problems down the road... y/m/d tends to be more universal, as there's no ambiguity)

Re: formatting mysql timestamp
by borisz (Canon) on Jun 19, 2005 at 09:21 UTC
    Yiou might try this to convert a string into something else:
    $_= '20050519112656'; s/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/$2-$3-$1 $4:$5:$6/; print __OUTPUT__ 05-19-2005 11:26:56
    Boris
Re: formatting mysql timestamp
by TedPride (Priest) on Jun 19, 2005 at 22:27 UTC
    EDIT: I just realized, this only converts to a timestamp, not to a formatted date. DATE_FORMAT(), as mentioned above, is what you need. Ignore my post.

    ---------------

    From http://www.mysqlfreaks.com/statements/68.php.

    UNIX_TIMESTAMP:

    Statement Info:
    If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' GMT) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' GMT. date may be a DATE string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or YYYYMMDD in local time.
    Examples:
    SELECT UNIX_TIMESTAMP();
    -> 882226357

    SELECT UNIX_TIMESTAMP('1997-10-04 22:23:00');
    -> 875996580

    So, you should be able to do something like UNIX_TIMESTAMP(fieldname) and have it convert for you. Why go through all the bother of doing it yourself?

Re: formatting mysql timestamp
by kaif (Friar) on Jun 21, 2005 at 07:40 UTC

    You may like some of the date-related modules mentioned on The Perl Advent Calendar. See the articles on Date::Parse and DateTime.

    However, my absolute favorite modules for this is Date::Manip. It does an amazing job of recognizing almost any date format. Here's some working code for your example:

    $ perl -MDate::Manip -e'print UnixDate("20050519112656","%m-%d-%Y %I:% +M %p %Z\n")' 05-19-2005 11:26 AM EDT

    Notice that I didn't have to say what format the input was in!