Getting a URL's publish/change date programatically

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
eastsidedev
Forum Newbie
Posts: 6
Joined: Tue Feb 10, 2009 2:02 pm

Getting a URL's publish/change date programatically

Post by eastsidedev »

I am starting to work with web page, and I need to programatically get a page's creation date and/or modification date. Would anyone have a code fragment (or a pointer) that can do that.

Regards,
Joseph
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Getting a URL's publish/change date programatically

Post by Benjamin »

Code: Select all

 
filemtime('/path/to/file');
 
eastsidedev
Forum Newbie
Posts: 6
Joined: Tue Feb 10, 2009 2:02 pm

Re: Getting a URL's publish/change date programatically

Post by eastsidedev »

astions wrote:

Code: Select all

 
filemtime('/path/to/file');
 
This just tells me how to get a file date/time, not how I can get a web page's creation date/time. Note that all I have is the URLs.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Getting a URL's publish/change date programatically

Post by John Cartwright »

Can you be specifc in your meaning of a webpage? Does "webpage" mean individual files on a domain, or the domain itself?

http://ca.php.net/manual/en/function.fi ... .php#73747 can look up the last modified of remote files
eastsidedev
Forum Newbie
Posts: 6
Joined: Tue Feb 10, 2009 2:02 pm

Re: Getting a URL's publish/change date programatically

Post by eastsidedev »

John Cartwright wrote:Can you be specifc in your meaning of a webpage? Does "webpage" mean individual files on a domain, or the domain itself?

http://ca.php.net/manual/en/function.fi ... .php#73747 can look up the last modified of remote files
Here's what I mean. I have a bunch of URLS:
http://www.microsoft.com/security/default.mspx
http://www.cisco.com/en/US/products/hw/ ... index.html
etc.....

and I would like to know when these pages were created or modified.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Getting a URL's publish/change date programatically

Post by John Cartwright »

Did you look at the link I gave you?
eastsidedev
Forum Newbie
Posts: 6
Joined: Tue Feb 10, 2009 2:02 pm

Re: Getting a URL's publish/change date programatically

Post by eastsidedev »

John Cartwright wrote:Did you look at the link I gave you?
Oops. Need new glasses, for some reason I thought this was part of your signature. I will try the link. Thx.
eastsidedev
Forum Newbie
Posts: 6
Joined: Tue Feb 10, 2009 2:02 pm

Re: Getting a URL's publish/change date programatically

Post by eastsidedev »

John Cartwright wrote:Did you look at the link I gave you?
I looked at it and tried it. It does not work, because there is no file to open if I don't know the exact default file name (index.php, index.html, default.html, etc.).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Getting a URL's publish/change date programatically

Post by John Cartwright »

You do not need to specify an actual file, only an http address. Although, thinking about it more it is up to the server to disclose the last modified header. In fact, when I tested it on this forums they did not report the last modified date. You can examine the headers to see for youself.

Code: Select all

function GetRemoteLastModified( $uri )
{
    // default
    $unixtime = 0;
   
    $fp = fopen( $uri, "r" );
    if( !$fp ) {return;}
   
    $MetaData = stream_get_meta_data( $fp );
 
    foreach( $MetaData['wrapper_data'] as $response )
    {
        // case: redirection
        if( substr( strtolower($response), 0, 10 ) == 'location: ' )
        {
            $newUri = substr( $response, 10 );
            fclose( $fp );
            return GetRemoteLastModified( $newUri );
        }
        // case: last-modified
        elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' )
        {
            echo $response;
            exit();
            
            $unixtime = strtotime( substr($response, 15) );
            break;
        }
    }
    fclose( $fp );
    return $unixtime;
}
 
echo GetRemoteLastModified('http://forums.devnetwork.net');
eastsidedev
Forum Newbie
Posts: 6
Joined: Tue Feb 10, 2009 2:02 pm

Re: Getting a URL's publish/change date programatically

Post by eastsidedev »

Yes, I kept getting a 0.

John Cartwright wrote:You do not need to specify an actual file, only an http address. Although, thinking about it more it is up to the server to disclose the last modified header. In fact, when I tested it on this forums they did not report the last modified date. You can examine the headers to see for youself.

Code: Select all

function GetRemoteLastModified( $uri )
{
    // default
    $unixtime = 0;
   
    $fp = fopen( $uri, "r" );
    if( !$fp ) {return;}
   
    $MetaData = stream_get_meta_data( $fp );
 
    foreach( $MetaData['wrapper_data'] as $response )
    {
        // case: redirection
        if( substr( strtolower($response), 0, 10 ) == 'location: ' )
        {
            $newUri = substr( $response, 10 );
            fclose( $fp );
            return GetRemoteLastModified( $newUri );
        }
        // case: last-modified
        elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' )
        {
            echo $response;
            exit();
            
            $unixtime = strtotime( substr($response, 15) );
            break;
        }
    }
    fclose( $fp );
    return $unixtime;
}
 
echo GetRemoteLastModified('http://forums.devnetwork.net');
Post Reply