Php find earliest date in the format dd/mm/yyyy in a text file

I have a text file in the following format


03/12/2023 06 This is the text in the line
03/12/2023 06 This is the text in the line
03/12/2023 06 This is the text in the line
03/12/2023 06 This is the text in the line
22/11/2023 06 This is the text in the next line
22/11/2023 06 This is the text in the next line
03/11/2023 06 This is another text in the line
03/11/2023 06 This is another text in the line
03/11/2023 06 This is another text in the line
04/12/2023 06 This is yet another text in the line
04/12/2023 06 This is yet another text in the line

Which is read into an array $data, from this array, I want to output the earliest date which in this case would be 03/11/2023

I would start with the code I gave in your previous thread - Php combine identical lines in text file and change the _parse() function so that it adds a column with a sortable date in a yyyy-mm-dd format, then simply call the usort() function to sort the data using this new column.

Thanks, Is there a function in php to reverse mm/dd/yyyy to yyyy/mm/dd in the array to do the sort?

You could just explode() it on the / character then concatenate the parts back together in the correct order.

Would it not be the very first or last line, depending on how you are logging, in the file?

This what I am using

$d = preg_replace ("~^(.{{$width}})(.+)~", ‘\1’, $data);
rsort($d);
$last_date = $d[0];

Which gives me

Array
(
    [0] => 20/12/2023
    [1] => 20/12/2023
    [2] => 20/12/2023
    [3] => 20/12/2023
    [4] => 20/12/2023
    [5] => 20/12/2023
    [6] => 20/12/2023
    [7] => 20/12/2023
    [8] => 20/12/2023
    [9] => 20/12/2023
    [10] => 20/12/2023
    [11] => 19/12/2023
    [12] => 19/12/2023
    [13] => 19/12/2023
    [14] => 19/12/2023
    [15] => 19/12/2023
    [16] => 18/12/2023
    [17] => 18/12/2023
    [18] => 18/12/2023
    [19] => 18/12/2023
    [20] => 17/12/2023
    [21] => 17/12/2023
    [22] => 17/12/2023
    [23] => 17/12/2023
    [24] => 17/12/2023
    [25] => 16/12/2023
    [26] => 16/12/2023
    [27] => 16/12/2023
    [28] => 16/12/2023
    [29] => 14/12/2023
    [30] => 14/12/2023
    [31] => 13/12/2023
    [32] => 13/12/2023
    [33] => 12/12/2023
    [34] => 12/12/2023
    [35] => 12/12/2023
    [36] => 12/12/2023
)

So it does not sort in date order even with sort so I cannot use the first or last

That’s how you are currently processing the file. But how is the file created and appended to?

Generally you will do something like appending to the existing file, or creating new depending on constraints. So logically the first line in the file should be the oldest and the last line should be the newest.

Using this code

    // Print the totals for each category
$data = file('/output/report_sort.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
	$data = str_replace('.mp3', '',  $data);
usort($data, fn($a, $b)=>substr($a,11,2)<=>substr($b,11,2));              // sort by code number
$codes = [];
foreach ($data as $d) {
    $p = strpos($d, ')');
    $k = substr($d, 11, $p-10);
    if (!isset($codes[$k])) {
        $codes[$k] = 1;
    } else {
        $codes[$k]++;
    }
}
```Then I use the lines above to extract the first 11 date characters from $data into $d, now I am using sort as in 

$width = 10;
$d = preg_replace ("~^(.{{$width}})(.+)~", ‘\1’, $data);
sort($d);
$last_date = $d[0];

What I see is it is working when all the months are the same but when another month is introduced into the data i.e. 20/11/2023 that is when the sort does not work

When you do the callback function for comparing the date, cast them as dates rather than strings.

The method I showed in the previous thread of - parse the data at the earliest point, then operate on the parsed data, was to allow you to operate on the data as a set, using php array functions, instead of creating a bespoke solution for each different operation.

Doing this for the current operation, would mean adding a few lines to the _parse() call-back function to produce a sortable date column, sorting the data on the sortable date column, then getting the earliest entry -

$file = 'somefile.txt';

$data = file($file, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);

// call-back function to parse each line of data into an array
function _parse($line)
{
	$arr['date'] = substr($line,0,10);
	
	// make a sortable date column
	$parts = explode('/',$arr['date']);
	// original is apparently - dd/mm/yyyy
	$arr['sdate'] = "$parts[2]-$parts[1]-$parts[0]";
	
	$arr['code'] = substr($line,11,2);
	
	// the rest of this doesn't seem to match the current example line format
	/*
	$p = strpos($line, ')');
	$arr['title'] = substr($line, 14, $p-13);
	$parts = explode(' ',substr($line, $p+2));
	$arr['file'] = $parts[0];
	$arr['location'] = implode(' ',array_slice($parts,1));
	*/
	return $arr;
}

// parse the data at the earliest point, then operate on the parsed data
$data = array_map('_parse',$data);

// sort by the 'sdate' sortable date column
usort($data, fn($a, $b)=>$a['sdate']<=>$b['sdate']);

// get the zeroth entry, earliest date
$earliest = $data[0];

// examine the result
echo '<pre>'; print_r($earliest); echo '</pre>';

Here’s another example. Your code indicates you are sorting on the ‘code’ column, then getting a count per code. Here’s what that would look like operating on the data as a set using php array functions -

$file = 'somefile.txt';

$data = file($file, FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);

// call-back function to parse each line of data into an array
function _parse($line)
{
	$arr['date'] = substr($line,0,10);
	
	// make a sortable date field
	$parts = explode('/',$arr['date']);
	// original is apparently - dd/mm/yyyy
	$arr['sdate'] = "$parts[2]-$parts[1]-$parts[0]";
	
	$arr['code'] = substr($line,11,2);
	
	// the rest of this doesn't seem to match the current line format
	/*
	$p = strpos($line, ')');
	$arr['title'] = substr($line, 14, $p-13);
	$parts = explode(' ',substr($line, $p+2));
	$arr['file'] = $parts[0];
	$arr['location'] = implode(' ',array_slice($parts,1));
	*/
	return $arr;
}

// parse the data at the earliest point, then operate on the parsed data
$data = array_map('_parse',$data);

// sort by the 'code' column
usort($data, fn($a, $b)=>$a['code']<=>$b['code']);

// get a count per 'code'
$result = array_count_values(array_column($data,'code'));

// examine the result
echo '<pre>'; print_r($result); echo '</pre>';

How do I cast $d my file of text dd/mm/yyyy lines into real dates?

As already stated -

Code -

Sponsor our Newsletter | Privacy Policy | Terms of Service