Forum Moderators: coopster

Message Too Old, No Replies

Generate a "new" or "updated" icon based on file date

Newbie alert

         

MrWumpus

10:03 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



I'm completely new to PHP, and what I'm trying to do is use PHP for basic templating of a new, fairly simple site. When I create or update pages on this site, I'd like to be able to automatically add a "new" or "updated" icon somewhere on the page. I already found a PHP script that properly returns the date of the script that includes it:

<?php
$file = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $file);
$pfile = $break[count($break) - 1];
//echo $pfile;
echo "This file was last modified on: " .date("d/m/Y",filemtime($pfile));
?>

Now I'd like it to be able to do the following:

    Set two variables ($fnew, $fupd) that contain the number of days I consider new or updated.
    Grab the date created from the file.
    Grab the date modified from the file.
    Echo the "new" icon code if the file was created in the last $fnew days.
    Echo the "updated" icon code if the file was modified in the last $fupd days.

I'm sure the answer is buried somewhere on php.net but I could not find it. I imagine this might take more code than I expect. Also I'm using XAMPP on Windows XP right now if that matters.

Thanks for any pointers or code samples!

[edited by: MrWumpus at 10:04 pm (utc) on Jan. 29, 2008]

cameraman

1:03 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See where you're using the date() function? You're converting a unix timestamp into a format that humans find useful. Turns out that for manipulating dates & times, timestamps are hard to beat because you can do all kinds of math to them without having to worry about 30 days past September (or whatever it is, I could never get that memorized).

Look at strtotime() [php.net]. This is a very handy function that goes the other way - it converts "plain text" into timestamps. For example:
strtotime("-30 days");

will give you the timestamp for thirty days ago. So you could easily compare the timestamp you get from filemtime() to the timestamp produced by strtotime to figure out if the file has been modified in the last 30 days.

$lastmonth = strtotime("-30 days");
$mtime = filemtime($pfile);

if($mtime < $lastmonth)
echo "This file hasn't been modified in over 30 days";
else
echo "This file has been changed within the last 30 days";

MrWumpus

7:26 am on Jan 30, 2008 (gmt 0)

10+ Year Member



cameraman, thank you for that solution! PHP is turning out to be more clever and friendly than I expected.

Your example works just fine with filemtime() and I see on php.net that there is also filectime() function that returns creation date, although filectime does not work properly on Win32.

Now off to the paint program to make those new and updated GIFs!

cameraman

7:30 am on Jan 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL cool!
Uh oh, another one hooked - MsWumpus about to become a php-widow...