Gossamer Forum
Home : General : Perl Programming :

Date Format

Quote Reply
Date Format
I am trying to print a date (taking the date from a mysql table which is stored in 2000-12-02) to 02-Dec-2000 using this code:
my ($year, $mon, $day) = split (/-/, $rec{'date_listed'});
my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
$rec{'date_listed'} = "$day-$months[$mon]-$year";


The problem I am having is that 2000-11-25 shows as 25-Dec-2000 and 2000-12-02 shows as 02--2000 anyone have any ideas what I've done wrong?


Quote Reply
Re: Date Format In reply to
Don't know if this will help...but I am using the following sub in Links SQL:

Code:

sub _unix_to_link_date {
# --------------------------------------------------------
# Convert Unix to Link Date

my $in = shift;
my %months = qw!1 Jan. 2 Feb. 3 Mar. 4 Apr. 5 May 6 Jun. 7 Jul. 8 Aug. 9 Sep. 10 Oct. 11 Nov. 12 Dec.!;
my ($year, $mon, $day) = split /-/, $in;
return "$months{int $mon} $day, $year";
}


Then to translate the date, I use the following codes:

Code:

my $newdate = &_unix_to_link_date($rec{'Add_Date'});


Then to print the date, simply use $newdate.

Regards,

Eliot Lee
Quote Reply
Re: Date Format In reply to
Thanks Eliot that did the trick.

Quote Reply
Re: Date Format In reply to
You're welcome.

Regards,

Eliot Lee
Quote Reply
Re: Date Format In reply to
Look at this page on MySQL's web site:
http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html#Date_and_time_functions

There are alot of functions that work in the SQL query to do just what you need it to without any extra code:
example: "SELECT DATE_FORMAT(columnname,'%d-%b-%Y'),* FROM table" will take the 20010201 data and return 01-Feb-2001 already formatted. Sweet huh?
hope this helps.

Joe Hofeditz


Quote Reply
Re: Date Format In reply to
Yea...nice.

Although the above method I posted is more flexible, because you can format the MONTH in any format you like (even foreign languages). The codes you provided will only output Feb rather than February or other formats.

Regards,

Eliot Lee
Quote Reply
Re: Date Format In reply to
Actually, the MySQL code is very flexible.
There are 30 different codes to output in any format.
The format string allows you to write the date any way you want and insert weekday, day of year,AM/PM... you name it.
For example: DATE_FORMAT(today,"%W, %M %D %Y %k:%i:%S%p week #%U")
will output:
Monday, February 5 2001 10:52:34AM week #6
You can format it anyway you want and it keeps track of everthing. Very simple.

thanks,
Joe

Quote Reply
Re: Date Format In reply to
Looks to me like the same amount codes either in Perl or MySQL offers the same result...again, I believe that Perl is a lot easier to understand and code than hard-coding the format via MySQL.

Regards,

Eliot Lee