jasonc Posted November 6, 2009 Share Posted November 6, 2009 I wish to get the file name which has the most recent date in it. all files are in the following format... (dd/mm/yyy) abc - 21-09-2009 - Agenda.pdf but it is not getting the correct file for some reason, this is what code i am using to get the filename. $files = glob('agendas/*.pdf'); $datestring = strtotime(basename(substr($files[0], 14, 10),".pdf")); $anchortext = date('j M Y',$datestring); // echo(".".basename(substr($files[0], 14, 10),".pdf")."."); $url = date('dmY',$datestring); if (strlen(date('j',$datestring)) == 1) { ?> <? } ?><strong><?=$anchortext;?></strong> Link to comment https://forums.phpfreaks.com/topic/180569-solved-grab-name-of-file-with-most-recent-date-in-it/ Share on other sites More sharing options...
MadTechie Posted November 6, 2009 Share Posted November 6, 2009 I would probably take this approach $sortfile = array(); foreach($files as $file){ //Extract date if (preg_match('/(\d+)-(\d+)-(\d+)[^.]*?\.pdf/', $file, $reg)) { //add date to array (file as key) $sortfile[$file] = mktime(0,0,0,$reg[2],$reg[1],$reg[3]); } } asort($sortfile); //Sort an array (the dates) and maintain index association (file names) //echo files foreach($sortfile as $file => $fdate){ echo "$file<br>"; } EDIT: updated regex and changed strtotime to mkdate please note this is untested, but the last file should be the one you want, or use arsort and it will be the first one Link to comment https://forums.phpfreaks.com/topic/180569-solved-grab-name-of-file-with-most-recent-date-in-it/#findComment-952652 Share on other sites More sharing options...
jasonc Posted November 6, 2009 Author Share Posted November 6, 2009 hey thanks that sorted them in reverse order but how do i get the first file name in the array? i tried $sortfile[0] and $sortfile[1] but this did not work Link to comment https://forums.phpfreaks.com/topic/180569-solved-grab-name-of-file-with-most-recent-date-in-it/#findComment-952692 Share on other sites More sharing options...
MadTechie Posted November 6, 2009 Share Posted November 6, 2009 your need to get the key, IE $sortfile = array_keys($sortfile); echo $sortfile[0]; Link to comment https://forums.phpfreaks.com/topic/180569-solved-grab-name-of-file-with-most-recent-date-in-it/#findComment-952697 Share on other sites More sharing options...
jasonc Posted November 6, 2009 Author Share Posted November 6, 2009 thats done it... $sortfile = array_keys($sortfile); echo $sortfile[0]; thanks for your help. Link to comment https://forums.phpfreaks.com/topic/180569-solved-grab-name-of-file-with-most-recent-date-in-it/#findComment-952701 Share on other sites More sharing options...
jasonc Posted November 6, 2009 Author Share Posted November 6, 2009 this is how i made the post set to 'solved' [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/180569-solved-grab-name-of-file-with-most-recent-date-in-it/#findComment-952889 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.