Check if a file is older than 2 days

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Check if a file is older than 2 days

Post 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');
}
?> 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Check if a file is older than 2 days

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

Re: Check if a file is older than 2 days

Post 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.
Post Reply