Jump to content

Recommended Posts

Is there a short php script that can look at the date of when a directory was last edited?

On our website, we have a home-made type photo gallery, and I want to be able to have the text "NEW" next to our photo gallery link every time new pictures are put in, and I want that link to be there for at least 5 days. I figure that a script that checks the date on a directory would be a good start.
Link to comment
https://forums.phpfreaks.com/topic/5104-php-directory-date-check/
Share on other sites

[!--quoteo(post=355658:date=Mar 16 2006, 02:26 PM:name=theycallmepj)--][div class=\'quotetop\']QUOTE(theycallmepj @ Mar 16 2006, 02:26 PM) [snapback]355658[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Is there a short php script that can look at the date of when a directory was last edited?

On our website, we have a home-made type photo gallery, and I want to be able to have the text "NEW" next to our photo gallery link every time new pictures are put in, and I want that link to be there for at least 5 days. I figure that a script that checks the date on a directory would be a good start.
[/quote]

Maybe a wild idea, but how about using php to pick up the names of the files and the dates they were modified within the directory. If a file is older than 5 days don't display the "NEW" icon (or text).

If you check the date on the directory, I don't think that adding files to the directory counts...I could be mistaken as I have never worked with that function in PHP before. I never had a use for it myself. I would check the functions for last modified on files/directories to find the one that fits your need.
I found a way for php to check the date on when a directory was last updated, and the directory does update every time a file is modified or added.

[code]<?php

// Change to the name of the file/directory
$last_modified = filemtime("images");

// Display the results
// eg. Last modified Monday, 21st March, 2006 @ 03:32pm
print "Last modified " . date("l, dS F, Y @ h:ia", $last_modified);

?>[/code]

Now my question is, how do I take the information that is outputed by that and make an if/else statement?

I want to be able to output an image or text "NEW" if the date is newer than 5 days, and if it is older than 5 days, I don't want it to output anything.

Any ideas on how I could do this?
I figured out how to do it!

I have it so it takes the number day out of the date and then subtract today's date by the modified date


[code]<?

// Change to the name of the file/directory
$last_modified = filemtime("/var/www/html/image_gallery");
$date = date("d ");
$mod_date = date("d ", $last_modified);
$diff_date = $date - $mod_date;

if ($diff_date <= 5) {
echo "NEW PICTURES!";
}else {
echo "No New Pictures!";
}
?>[/code]

It seems to work good.
Not quite. See your logic is flawed. If there have been no new pictures in three months but today is the 9th day of the 4th month and there were new pictures on the 6th day of the 1st month...9-6=3 3<=5....which means new pictures, right?...need to make sure the months and years are appropriate too.
Here, I added the months and years so it should all work now

[code]<?
// Change to the name of the file/directory
$last_modified = filemtime("images");
$ddate = date("d");
$mdate = date("m");
$ydate = date("y");
$dmod_date = date("d", $last_modified);
$mmod_date = date("m", $last_modified);
$ymod_date = date("y", $last_modified);
$diff_date = $ddate - $dmod_date;

if ($diff_date <= 5 && $mdate == $mmod_date && $ydate == $ymod_date) {
echo "<font color=red><b>NEW PICTURES!</b></font>";
}else {
echo "No New Pictures!";
}
?>[/code]
What you should do is to convert the modification time and the current date to integers. Subtract the modification time from the current time and divide by 86400 (the number of seconds in a day). If the result is 5 of less, you have a new file.

[code]<?php
$last_modified = filemtime("/var/www/html/image_gallery");
$today = strtotime("today");
$days_since = ($today - $last_modified) / 86400;
?>[/code]

Ken

What you should do is to convert the modification time and the current date to integers. Subtract the modification time from the current time and divide by 86400 (the number of seconds in a day). If the result is 5 of less, you have a new file.

[code]<?php
$last_modified = filemtime("/var/www/html/image_gallery");
$today = strtotime("today");
$days_since = ($today - $last_modified) / 86400;
?>[/code]

Ken
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.