theycallmepj Posted March 16, 2006 Share Posted March 16, 2006 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 Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/ Share on other sites More sharing options...
fusionpixel Posted March 16, 2006 Share Posted March 16, 2006 [!--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). Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-18161 Share on other sites More sharing options...
txmedic03 Posted March 17, 2006 Share Posted March 17, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-18184 Share on other sites More sharing options...
theycallmepj Posted March 20, 2006 Author Share Posted March 20, 2006 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:32pmprint "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? Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19132 Share on other sites More sharing options...
theycallmepj Posted March 20, 2006 Author Share Posted March 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19151 Share on other sites More sharing options...
txmedic03 Posted March 21, 2006 Share Posted March 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19257 Share on other sites More sharing options...
theycallmepj Posted March 21, 2006 Author Share Posted March 21, 2006 That makes sence, thanks, I'll work on putting the months in Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19426 Share on other sites More sharing options...
theycallmepj Posted March 21, 2006 Author Share Posted March 21, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19438 Share on other sites More sharing options...
kenrbnsn Posted March 21, 2006 Share Posted March 21, 2006 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]KenWhat 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 Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19440 Share on other sites More sharing options...
txmedic03 Posted March 21, 2006 Share Posted March 21, 2006 I like ken's method...simplicity... :-) Quote Link to comment https://forums.phpfreaks.com/topic/5104-php-directory-date-check/#findComment-19460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.