Page 1 of 1

Check if a file is older than 2 days

Posted: Wed Jun 16, 2010 11:47 am
by mrlayance
Code should check a file a echo based on if it is older then 2 days.

Can anyone see what I am doing worng?

Code: Select all

<?php
$filename = 'css.css';

if ($timestamp = strtotime("+2 days")) {
    echo "Script has not run for 2 days";
} else {
    echo "$filename == " . date('l dS \o\f F Y h:i:s A');
}
?> 

Re: Check if a file is older than 2 days

Posted: Wed Jun 16, 2010 12:03 pm
by AbraCadaver
No offense, but I can't see anything that you are doing right. Try this:

Code: Select all

$filename = "css.css";
$filetime = filemtime("/path/to/$filename");

if (strtotime("+2 days", $filetime) < time()) {
    echo "OLDER THAN 2 DAYS";
} else {
    echo "NOT OLDER THAN 2 DAYS";
}
You could also use:

Code: Select all

if($filetime * 172800 < time()) {
Though you use the phrase "has not run for 2 days", in which case that would be the access time fileatime() instead of the modification time filemtime().

Re: Check if a file is older than 2 days

Posted: Wed Jun 16, 2010 12:41 pm
by mrlayance
AbraCadaver wrote:No offense, but I can't see anything that you are doing right. Try this:

Code: Select all

$filename = "css.css";
$filetime = filemtime("/path/to/$filename");

if (strtotime("+2 days", $filetime) < time()) {
    echo "OLDER THAN 2 DAYS";
} else {
    echo "NOT OLDER THAN 2 DAYS";
}
You could also use:

Code: Select all

if($filetime * 172800 < time()) {
Though you use the phrase "has not run for 2 days", in which case that would be the access time fileatime() instead of the modification time filemtime().
No offence taken, i'm a noob.

Thanks for the help.