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
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
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
Share on other sites

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.