Re: sorting dates

From: Date: Fri, 07 Jul 2000 23:40:12 +0000
Subject: Re: sorting dates
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-5477@lists.php.net to get a copy of this message
In article <11692891.962842170207.JavaMail.imail@scorch.excite.com>, jeremyf10@excite.com wrote: > Is there a way to sort a flat text file by the date. Example: > > December 12 blahblahblah > Jan 12 blah > Feb 10 blah > June 4 blah > November 1 blah > october 1 blah > > Sorting would result in: > > Jan 12 blah > Feb 10 blah > June 4 blah > october 1 blah > November 1 blah > December 12 blah > > Is this possible using php? <?php $file = file('/path/to/text/file') or die("Could not open file"); reset($file); while (list(,$line) = each($file)){ $month = strtok($line, ' '); $day = strtok(' '); switch(strtolower($month)){ case 'january': case 'jan': $month = 1; break; case 'february': case 'feb': $month = 2; break; . . . default: //force any bad month to the top of the list, I guess... $month = 0; } $date = mktime(0, 0, 0, $month, $day); $lines[$date] = $line; } sort($lines); reset($lines); while (list(,$line) = each($lines)){ echo $line, "<BR>\n"; } ?> You're still at the mercy of typos in month names, but they'll appear at the top of the list as a result of the 'default' case above. -- Richard Lynch | If this was worth $$$ to you, buy a CD US Customer Support Director | from one of the artists listed here: Zend Technologies USA | http://www.L-I-E.com/artists.htm http://www.zend.com | (this has nothing to do with Zend, duh!)

« previous php.general (#5477) next »