Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder files by unconventional date in filename (PHP)
    text
    copied!<p>I'm working with a PHP script that allows the chronological browsing of Eggdrop-generated IRC logs. Initially, I was reading the directory contents and inserting the log names into an array based on file modification date. After a recent server move, however, the files have had their modification dates updated and the navigation is now disorderly!</p> <p>The log filename structure looks like:</p> <pre><code>channel.log.dayMONTHyear.txt </code></pre> <p>for example:</p> <pre><code>shrawberry.log.08Apr2011.txt </code></pre> <p>which, for being quite human-readable, is difficult to order properly.</p> <p>Since the month code is always three characters long and comes in a predictable position in the sequence, I could manually parse the nonstandard date code into a Unix timestamp, iterate through the list and add each item to an array with that timestamp, and then sort the array by that number.</p> <p>But that sounds excessive.</p> <p>Am I on the money, or is the solution I proposed ideal?</p> <hr> <p>With Marc B.'s help, I've implemented the following:</p> <pre><code>function dateFromEggLog($string){ $month = substr($string,-11,-8); $day = substr($string,-13,-11); $year = substr($string,-8,-4); for($i=1;$i&lt;=12;$i++){ if(strtolower(date("M", mktime(0, 0, 0, $i, 1, 0))) == strtolower($month)){ $month = $i; break; } } return "$year-$month-$day"; } function my_compare($a, $b) { $a_date = dateFromEggLog($a); $b_date = dateFromEggLog($b); if ($a_date == $b_date) { return 0; } $a = strtotime($a_date); // convert to PHP timestamp $b = strtotime($b_date); // convert to PHP timestamp return (($a &lt; $b) ? -1 : 1); } </code></pre> <p>This successfully sorts my logs, without needing to muck around with my array.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload