Forum Moderators: coopster & phranque

Message Too Old, No Replies

Sort by date

Novice needs help. Please

         

Charles_cz

1:17 am on Jul 4, 2003 (gmt 0)

10+ Year Member



I need some hints.
I have flat database.

Example:
$name::$address::$date::$team
Charles,Italy,1 Jan 03,Skunks

I record every day new data. Then I use script to display data for diferent purposes. I can't figure out how to sort all data for team XXX by date.

I can make array with all record for $team but I have no idea how to sort data in array by date. I guess I am on wrong track. :-(

In what format should I record $date (date of record) so I can use it later to sort data by date?

I am really new to CGI and there is probably very simple solution.

thanks for help
Charles

Robber

8:23 am on Jul 4, 2003 (gmt 0)

10+ Year Member



Its a while since I've done much with dates in Perl, but you might need to find a Date module that can handle ordering dates.

I would think one ships with the default install, if not check out the cpan archive to see if there is anything appropriate.

Oh yes, and welcome to WW.

sugarkane

12:49 pm on Jul 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your best bet would be to convert the date into a unix timestamp format, which you can then sort with a plain numerical sort. The function you'd use to generate a timestamp is mktime(), included as standard with Perl in the POSIX module.

use POSIX;
$timestamp = mktime($seconds,$hours,$minutes,$date,$month,$year,0,0,0);

Once you've finished the sorting, you can convert a timestamp back into a 'human' format with the time() function, eg:


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=time($timestamp);
@days=("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
@months=("Jan","Feb","Mar","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec");
$year+=1900;
if ($sec < 10) {$sec="0".$sec;}
if ($min < 10) {$min="0".$min;}
$displaytime="$days[$wday], $mday $months[$mon] $hour:$min";

tschild

5:48 pm on Jul 9, 2003 (gmt 0)

10+ Year Member



I'd also go with numerical representation of the date because this makes for efficient sorting.

If your date is input as text you could e.g. use the parse_date function from HTTP::Date or the Decode_Date_EU
function from Date::Calc to parse the text and extract a numerial date.