PDA

View Full Version : function to convert from timestamp to nth mm yyyy


bazz
05-30-2008, 10:27 PM
Hi,

I am not getting this to work so I ask for your help, please.


# convert a unix timestamp to a string as 'May 21st 2008';
# for example
sub date_conversion {
use Date::Parse;
my $timestamp = shift;

#strftime uses "%a %b %d %H:%M:%S %Z %Y".
my $finished_date = strftime("%a %Z %Y",$timestamp);
return $finished_date;
}



Currently, this code brings upo this error and I don't understand it.


Usage: POSIX::strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) at script.pl line 678.

Any pointers most welcome


bazz

FishMonger
05-30-2008, 10:34 PM
What is the value of $timestamp

bazz
05-30-2008, 10:49 PM
OK, I have progressed to this but would like to have it read like this:

MON 21st Jan 2008 instead of 28 Jan 2008


sub date_conversion {
use Date::Parse;
my $timestamp = shift;

#strftime uses "%a %b %d %H:%M:%S %Z %Y".
#my $finished_date = strftime("%a %Z %Y",$timestamp);
my $finished_date = UnixDate(ParseDate("epoch $timestamp"), '%a %d %b %Y');

return $finished_date;




@FishMonger ; The time stamp is 1210806000

bazz

bazz
05-30-2008, 10:54 PM
I got it :)

I had fortgotten to add the 'use Date::Format; (or Date::Parse;) to the top. :o

bazz

FishMonger
05-30-2008, 11:01 PM
sub date_conversion {

return strftime( "%a %dst %b %Y", localtime( shift() ) );

}

bazz
05-30-2008, 11:20 PM
Thanks FishMonger,


as ever, you do it more efficiently. :) :thumbsup:

bazz

FishMonger
05-31-2008, 12:28 AM
The only problem with "my solution" is that I hard coded the 'st'. If you really need that in the date format, you need to make an adjustment that adds it (st nd rd th) dynamically.

bazz
05-31-2008, 02:43 PM
Thanks FishMonger; I saw that. I shall try to work out the dynamic th nd st rd before asking a question about it.

bazz