Page Creation and Modified Date

Posted By: Ian on Jun 24, 2011 in PHP, Snippets

Recently i was asked to create a script for someone to show the Page Creation and Modified Date of web pages and being as this was for a Linux server where there’s no function for the creation date that I’m aware of like this is for the modified date i had to create a little script to do just that which today i share with you in the hope it may be useful for someone else as well.

MySQL table


CREATE TABLE IF NOT EXISTS `CreatedDate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(100) NOT NULL,
`created` date NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `filename` (`filename`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

CSS


<style type="text/css">#dates {
font-family:Georgia;
color:rgba(69,54,37,0.6);
font-size:0.75em;
font-style:italic;
letter-spacing:0.25em;
border-top:1px solid rgba(69,54,37,0.2);
display:block;
padding-top:0.5em;
margin-top:2em;
width:400px;
text-align: center
}
</style>

PHP


<?php
// Database Variables (edit with your own server information)
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$db = '';
//
echo "<div id="dates"><span class="created-date">";
// Get full URL
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//Get filename including extension
$file = basename($_SERVER['REQUEST_URI']);
//
// Connect to Database
$connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to server ... n" . mysql_error());
mysql_select_db($db) or die("Could not connect to database ... n" . mysql_error());
//Select from the database the date where the filename is equal to this URL
$result = mysql_query("SELECT DATE_FORMAT(created, '%D %M %Y') FROM CreatedDate WHERE filename = '$url'");
while ($row = mysql_fetch_array($result)) {
// If the URL is found in the database print the date
print "This page was created on " . $row["DATE_FORMAT(created, '%D %M %Y')"] . "</span><br />";
}
// Else insert the URL and current date into the database and print the current date
if (mysql_affected_rows() == 0) {
$result = mysql_query("INSERT INTO CreatedDate(filename, created) VALUES('$url', CURDATE() ) ");
print "This page was created on " . date('d m y') . "</span><br />";
}
// If file exists show last modified date from the inode
if (file_exists($file)) {
echo "<span class="mod-date">last modified " . date("F d Y H:i:s.", filemtime($file)) . "</span></div>";
}
?>

Try Me

This is ideal for placing in the footer file for your website and then will automatically create a database entry for every page that gets loaded which includes the footer file

Ian

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

Contact Form

captcha

-
blank1
blank2
blank3
blank4
blank5
blank6
blank7
blank8
blank9
blank10
blank11
blank12
blank13
blank14
blank15