Jump to content

[SOLVED] Check File age.


Daleeburg

Recommended Posts

I am tring to make a script that i will turn into a cron job.

 

Idealily it would check the age of the file and then figure out if it is more then 48 hours old and if it is it will delete it.

 

i have figured out how to check the age and how to delete the files, but how would i find out if they are 48 hours old?

 

~D

Link to comment
https://forums.phpfreaks.com/topic/52017-solved-check-file-age/
Share on other sites

<?php
$filetime = filemtime($file);
$timenow = time();
$timetwodays = time()-3600*48; // two days ago

if (($timenow - $filetime) >= $timetwodays) {
   echo 'The file is older than two days.'
}else {
   echo 'file is not older than two days';
}
?>

 

Un-tested, I would suggest testing it first.

Link to comment
https://forums.phpfreaks.com/topic/52017-solved-check-file-age/#findComment-256393
Share on other sites

It almost worked, so here is the working script

 

<?php
$filetime = filemtime($file);
$timenow = time();
$twodays = 3600*48; // two days ago

if (($timenow - $filetime) >= $twodays) {
   echo 'The file is older than two days.'
}else {
   echo 'file is not older than two days';
}
?>

 

or

 

<?php
$filetime = filemtime($file);
$timetwodays = time()-3600*48; // two days ago

if ($filetime <= $timetwodays) {
   echo 'The file is older than two days.'
}else {
   echo 'file is not older than two days';
}
?>

(second is untested, but should work)

 

Thanks

~D

Link to comment
https://forums.phpfreaks.com/topic/52017-solved-check-file-age/#findComment-256415
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.