LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-09-2009, 08:35 AM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
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.
 
Old 02-09-2009, 09:46 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
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.

Last edited by Telemachos; 02-09-2009 at 12:16 PM. Reason: Changed my "American style" version to the Europe/British style with day before month
 
Old 02-09-2009, 05:58 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
How about using MySQL date_format() fn: http://dev.mysql.com/doc/refman/5.0/...on_date-format
 
Old 02-09-2009, 07:32 PM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by chrism01 View Post
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.)
 
Old 02-10-2009, 12:02 AM   #5
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
Quote:
Originally Posted by resetreset View Post
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.
 
Old 02-10-2009, 01:09 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Quote:
Originally Posted by burschik View Post
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
 
Old 02-11-2009, 06:24 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by Telemachos View Post
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
 
Old 02-11-2009, 07:14 AM   #8
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by graemef View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 04:20 AM
Java Epoch time to Human Readable (using Date) true_atlantis Programming 1 05-09-2008 05:13 PM
How to change mysql date format sidboyce Linux - Software 1 10-24-2007 07:25 AM
Webalizer - howto convert the trafic into human readable? sys7em Linux - Server 3 09-25-2006 04:48 AM
MySQL date format mnauta Linux - General 2 05-13-2004 06:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration