Jump to content


Photo

php directory date check


  • Please log in to reply
9 replies to this topic

#1 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 16 March 2006 - 08:26 PM

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.
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#2 fusionpixel

fusionpixel
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 16 March 2006 - 11:23 PM

[!--quoteo(post=355658:date=Mar 16 2006, 02:26 PM:name=theycallmepj)--][div class=\'quotetop\']QUOTE(theycallmepj @ Mar 16 2006, 02:26 PM) View Post[/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).


Did you know there is a manual for PHP? [a href="http://" target="_blank"]http://www.php.net[/a]
Did you know there is a manual for mySQL? [a href="http://" target="_blank"]http://www.mysql.org[/a]

#3 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 17 March 2006 - 12:40 AM

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.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#4 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 20 March 2006 - 08:34 PM

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.

<?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);

?>

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?
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#5 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 20 March 2006 - 09:23 PM

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


<?

// 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!";
}
?>

It seems to work good.
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#6 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 21 March 2006 - 07:24 AM

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.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#7 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 21 March 2006 - 07:24 PM

That makes sence, thanks, I'll work on putting the months in
Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#8 theycallmepj

theycallmepj
  • Members
  • PipPip
  • Member
  • 18 posts
  • LocationMontrose, PA

Posted 21 March 2006 - 08:09 PM

Here, I added the months and years so it should all work now

<?
// 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!";
}
?>

Thanks, Paul Travis
THEYCALLMEPJ.com
MASD.info

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 March 2006 - 08:19 PM

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.

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

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.

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

Ken

#10 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 21 March 2006 - 09:00 PM

I like ken's method...simplicity... :-)

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users