Sorting values in a text file by date?

Associate
Joined
29 Dec 2004
Posts
2,253
Hi there,

I have a text file which contains values like this:

flatfile.txt said:
15/10/2008 WS - Mr Tester. Some test details
04/11/2008 BH - Dr Feelgood. Some test details
12/11/2008 BH - Ms Test. Some test details
22/10/2008 BH - Mrs Jones. Some test details
30/09/2008 WS - Mr Smith. Some test details
01/10/2008 WS - Mr Pesky. Some test details
15/10/2008 WS - Mr Fitz. Some test details

I use PHP to display the list, but at the moment it is displayed as each line is read (basically it's displayed as shown above)...is there a way to sort it by the date so it is displayed with the nearest events first? Here is the bit of code i use to simply display the list as it is at the moment:

PHP:
<?php
$file_handle = fopen("../path/to/flatfile.txt", "rb");

while (!feof($file_handle) ) {
        $line_of_text = fgets($file_handle);
        echo '$line_of_text';
}

fclose($file_handle);
?>

Can anyone help?
 
if you read it in line by line and insert it into some form of data structure. i.e. you could insert it in order into an array, or just shove it all into a hash and read the keys in order. or some variation of the above.

basically what you have to do is:

read it in and store it
sort it
print it

daven
 
I got that from general Googling, but unfortunately none of the code i found helped me to do what i want - could you give some examples of code that i could adapt?
 
i was hoping to read the dates simply by using strtotime() to convert the first 10 characters of each line into a unix timestamp. but it doesn't work with uk formatted dates. so i had to faff about a bit. it works though. :p

PHP:
<?php
$array = file('test.txt');
foreach($array as $value) {
	$date = mktime(0, 0, 0, substr($value, 3, 2), substr($value, 0, 2), substr($value, 6, 4));
	$new_array[$date] = $value;
}
ksort($new_array); //put it in order, earliest first
foreach($new_array as $value) {
	echo $value . '<br />';
}
?>
 
Wow thanks for all that, really appreciate it - i seem to be getting this error though:

Warning: mktime() expects parameter 4 to be long, string given in /path/files.php on line 70

Warning: mktime() expects parameter 5 to be long, string given in /path/file.php on line 70

The exact contents of my text file is this as i expect this is the issue...i had to take the stars out before as the forum was processing them as bullet points, oh and unfortunately i have to have the "Projects" header at the top as the calendar application that is also using this data uses it to define things:

Code:
====== Projects =====

  * 15/10/2008 WS - Mr Tester. Some test details
  * 04/11/2008 BH - Dr Feelgood. Some test details
  * 12/11/2008 BH - Ms Test. Some test details
  * 22/10/2008 BH - Mrs Jones. Some test details
  * 30/09/2008 WS - Mr Smith. Some test details
  * 01/10/2008 WS - Mr Pesky. Some test details
  * 15/10/2008 WS - Mr Fitz. Some test details
 
awkward git. :D

go and look up the substr function in the php manual. :p

anyways...

PHP:
<?php
$array = file('test.txt');
//get rid of the first 2 lines. there might be a more elegant way? i dunno. 
unset($array[0]);
unset($array[1]);
$new_array = array();
foreach($array as $value) {
    $date = mktime(0, 0, 0, substr($value, 7, 2), substr($value, 4, 2), substr($value, 10, 4));
    if(array_key_exists($date, $new_array)) $date++; //stop records overwriting each other if we have multiple records with the same date
    $new_array[$date] = $value;
}
ksort($new_array); //put it in order, earliest first
foreach($new_array as $value) {
    echo $value . '<br />';
}
?>
 
Last edited:
I thought the substr part was the issue as i could see it was reffering to specific points of the text file to get the date...i did also look on PHP.net at the function but just couldn't get it exactly right, i then had to give up before my mind started to melt :p

All is working a treat now, i just need to put in some if statements to make sure it only prints results for this month or next month...but i enjoy the challenges i can do!

Many thanks for your help, you sir are a gentleman.
 
Back
Top Bottom