Avatar of tech1984
tech1984 asked on

having problems with comparing time & date using php.

I have a variable that is a date stamp that is basically the modification date of a file:

$file_date = date("mdY H:i:s", filemtime("$dir/$file"));

the above will be in a loop, that checks an entire directory. I need to write another variable that would represent 15 minutes ago that I can compare with the list of files. I am trying to echo all the files that have a file mod date >= fifteen minutes ago so it would look like this

$fifteen_minutes_ago = (now - 15 minutes)

if($file_date = date("mdY H:i:s", filemtime("$dir/$file")) >= $fifteen_minutes_ago;
echo "$file<br>";

obviously I am needing the code that should go (now - 15 minutes) that could be compared to the $file_date variable to see if $file_date should be listed.

Thanks for your time!

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Sorry, missed the additional * 60 ... this hopefully will work...

$fifteen_minutes_ago=time()-(15 * 60 * 60);

if ($file_date>$fifteen_minutes_ago)
echo "$file<br>";

Ok, the first one should be right 15 minutes * 60 seconds per minute

i wouldnt bother with  date("mdY H:i:s", filemtime("$dir/$file"));
just leave it as $file_date=filemtime("$dir/$file"); so you have an integer (number of seconds since unix epoch, which is 1/1/1970 or something
then 15 minutes ago is
$15minsago = mktime() - 54000; // its better to use 54000 than 15 * 60 * 60 as its less processing

full example below
$file_date = filemtime("$dir/$file");
$fifteen_minutes_ago = time() - 54000;
if ($file_date >= $fifteen_minutes_ago) {
    echo "$file<br />";

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

It's true that it's less processing, but I guarantee that you won't see the difference in speed and it's easier to read (php.net actually recommends it) http://us2.php.net/time ... see the break down there.   and the correct formula is 15 * 60 (900)  because there are 900 seconds in 15 minutes.

you're right about the 900.

i usually skip the multiplication but add a comment explaining that 900 is 15 * 60 seconds.

yes i agree you wont notice the difference, i think its a better habit to code for processing performance

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.