Ok so on a web application I needed to list the files in a directory, ordered by last modified date.

I was pulling them out in an array, fetching the filemtime() for each file, and trying to order them by that date. I was using asort() to sort the files in an array so they'd list chronologically.

Well that wasn't working as planned. Files were getting out of order. I figured asort() could have been having trouble ordering dates numerically, so I tried strtotime() on the dates first, and ordered the files by unix timestamp.

That didn't work either. I did a date() on the unix timestamp fetched by strtotime() and found that the dates were coming out inaccurately. 2003 was being changed to 2013, etc.

Well I gradually figured out that the date format output by filemtime() was not an acceptable natural language date format. filemtime() was fetching dates with a dash - ex. 07-08-2003. strtotime() was making incorrect timestamps because it doesn't read dates with dashes formatted that way. Apparently asort() reads dates the same way strtotime does.

I think this is kind of perverse... in order to fix it I had to do an ereg_replace("-","/",$date) in order to change the filemtime() date format to one properly readable by strtotime and asort.

*sigh*

Am I the only one who has noticed this? Is this something that can be corrected in PHP? (Perhaps the dates fetched by filemtime() etc can be changes to be formatted in a way that is acceptable to other date() functions??)


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to