1

I want to get a range of files with PHP glob function, but no older than a month (or other specified date range).

My current code:

$datetime_min = new DateTime('today - 4 weeks');

$product_files = array();
foreach(glob($image_folder.$category_folder.'*'.$img_extension) as $key => $product) if (filemtime($product)>$datetime_min) { $product_files[] = $product; }

This returns an error:

Notice: Object of class DateTime could not be converted to int

I think it still gives me a result with all files in that folder. So my approach could be completely wrong.

How can I make this code work, so I only have an array with files no older than the specified date?

Emiel
  • 33
  • 5
  • What have you tried to debug the problem? Are you sure this is in any way related to the usage of glob itself? – Nico Haase Feb 21 '21 at 13:44
  • This seemed to have resolved it: `$datetime = new DateTime('today - 4 weeks');` `$datetime_min = $datetime->getTimestamp();` The foreach and glob fuction I left unchanged. – Emiel Feb 22 '21 at 15:25

3 Answers3

2

filemtime() returns a Unix timestamp which is an integer. DateTime objects are comparable but only to each other. You'll need either need to convert them into Unix timestamps or convert the result of filemtime() into a DateTime object.

Option 1:

$datetime = (new DateTime('now'))->format('U');
$datetime_min = (new DateTime('today - 4 weeks')->format('U');

Option 2:

$filetime = new DateTime(@filemtime($product));
if (filetime > $datetime_min) {}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thanks John. Of course I should have realized the unix format :(. Although your code did threw me a server500 error. But with your timestamp suggestion I adapted it to this: '$datetime->getTimestamp();' Now it runs without an error. But I can not check if it works until after a few days when there are files with different dates in that folder. :) Anyway. Thanks again! – Emiel Feb 21 '21 at 13:27
0

Just try this script

<?php

$to = date("Y-m-d H:i:s");
$from = date('Y-m-d H:i:s', strtotime("-100 days"));

getAllFilesInDirectoryWithDateRange("*.*", $sdf, $today);

function getAllFilesInDirectoryWithDateRange($filePattern, $from, $to) {
    foreach (glob($filePattern) as $filename) {
        if (date("Y-m-d H:i:s", filemtime($filename)) >= $from &&
            date("Y-m-d H:i:s", filemtime($filename)) <= $to) {
                echo "$filename" . "\n";
        }
    }
}

output

test1.txt
test2.txt
test3.txt
test4.txt
test5.txt

You can use getAllFilesInDirectoryWithDateRange function and get all file names in a directory
In this function I use filemtime to get the time and then check the threshold like this

date("Y-m-d H:i:s", filemtime($filename)) >= $from && 
date("Y-m-d H:i:s", filemtime($filename)) <= $to
azibom
  • 1,769
  • 1
  • 7
  • 21
0

You can use array_filter() for this problem.

$tsLimit = strtotime('-2 Month');
$file_pattern = ..//your file pattern

$files = array_filter(
  glob($file_pattern), 
  function($val) use($tsLimit){
    return filemtime($val) > $tsLimit;
  }
);
jspit
  • 7,276
  • 1
  • 9
  • 17