LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Converting a date in MySQL format to human readable in Perl (https://www.linuxquestions.org/questions/programming-9/converting-a-date-in-mysql-format-to-human-readable-in-perl-703342/)

resetreset 02-09-2009 08:35 AM

Converting a date in MySQL format to human readable in Perl
 
Hi,
I have a variable, say $ary[3], which contains a MySQL date eg. "2001-1-19" for 19th Jan, 2001. How do I convert it in Perl to something more human, eg. "19th January, 2001"?

Thanks.

Telemachos 02-09-2009 09:46 AM

Take a look at the CPAN, in particular Class::Date or Date::Manip. There are lots of other time/date modules as well.

I got bored at work, so here's a quick example using Date::Calc:
Code:

#!/usr/bin/env perl
use strict;
use warnings;
use Date::Calc qw/Month_to_Text English_Ordinal/;

my $mysql = '2001-1-19';

my ( $year, $month, $day ) = split /-/, $mysql;

my $string = sprintf("%s %s, %d", English_Ordinal($day), Month_to_Text($month), $year);

print "$string\n";

Your exact problem shows up in one of the recipes.

chrism01 02-09-2009 05:58 PM

How about using MySQL date_format() fn: http://dev.mysql.com/doc/refman/5.0/...on_date-format

Telemachos 02-09-2009 07:32 PM

Quote:

Originally Posted by chrism01 (Post 3437724)
How about using MySQL date_format() fn: http://dev.mysql.com/doc/refman/5.0/...on_date-format

Heh - I would blame it on Resetreset (for asking about a Perl solution), but the truth is, I'm much more comfortable with Perl than databases anyhow. (Also, at a quick glance the MySQL date_format doesn't seem to offer the English_Ordinal convenience function to give 3rd for 3 and 1st for 1. But I imagine it's a lot faster.)

burschik 02-10-2009 12:02 AM

Quote:

Originally Posted by resetreset (Post 3437152)
Hi,
I have a variable, say $ary[3], which contains a MySQL date eg. "2001-1-19" for 19th Jan, 2001. How do I convert it in Perl to something more human, eg. "19th January, 2001"?

Thanks.

More human? You don't think you might be culturally biased? I'd guess most people in the world find ISO dates more readable than traditional English dates.

jlinkels 02-10-2009 01:09 AM

Quote:

Originally Posted by burschik (Post 3437959)
More human? You don't think you might be culturally biased? I'd guess most people in the world find ISO dates more readable than traditional English dates.

Haha, I am a strong advocate about using ISO date format since 1981. I have met nothing than opposition and puzzeled looks. Except for one company I worked for where it was company policy. Even in my current company where they use strictly ISO for broadcast schedules, they use dd-mm-yyyy for the regular administration.

jlinkels

graemef 02-11-2009 06:24 AM

Quote:

Originally Posted by Telemachos (Post 3437798)
at a quick glance the MySQL date_format doesn't seem to offer the English_Ordinal convenience function to give 3rd for 3 and 1st for 1.

The MYSQL DATE_FORMAT Specifier %D should do it for you

Telemachos 02-11-2009 07:14 AM

Quote:

Originally Posted by graemef (Post 3439525)
The MYSQL DATE_FORMAT Specifier %D should do it for you

Wow, I'm 0 for 2 in this thread. Thanks for pointing it out, he said while sheepishly making a note to read the f@#$ing manual more slowly next time.


All times are GMT -5. The time now is 11:33 AM.