PHP search array and return only first value that meets criteria -
i have directory of files labeled yyyy-mm-dd.jpg
.
using php, trying search through array of these files , print filename (without jpg) of latest date before today.
for example: today 2015-04-14
files:
- 2015-04-20.jpg <-in future
- 2015-04-11.jpg <-first file has date before today's date
- 2015-04-02.jpg
- 2015-04-01.jpg
i want return "2015-04-11".
i've been working on couple days, trying piece things multiple posts. so, i've gone through lot of iterations. here one:
$scan = scandir($comic_path,1); //go through directory desc $last_comic = substr($scan[0],0,10); //substr scan date function lastcomic(){ foreach ($scan $acomic) { if ($acomic < $last_comic) { echo $acomic . "\n"; } $last_comic = $acomic; }} lastcomic($last_comic);
thanks in advance help.
well, code in way:
$today = date("y-m-d"); $last_comic = substr($scan[0],0,10); foreach ($scan $acomic) { $only_date = substr($acomic,0,10); if ($only_date < $today) { $last_comic = $acomic; break; } }
is you're trying accomplish?
Comments
Post a Comment