PHP program to display the last visited date-time

By | June 21, 2017

PHP program to store current date-time in a cookie and display the Last visited date-time on the web page upon revisiting the same web page. The code is quite simple and small. We are making use of the setcookie() function where we will store the date and time value during the first visit of the user. During the second visit, we will just check if the cookie is already set, if yes then display the time stored in the cookie variable.

 

 

 

<html>
<body bgcolor="87ceeb">
<center><h2>Last visited time on the web page</h2></center>
<br>
<?php
//date_default_timezone_set('Asia/Calcutta'); - You can choose any timezone
//Calculate 60 days in the future
//seconds * minutes * hours * days + current time
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
if(isset($_COOKIE['lastVisit']))
{
$visit = $_COOKIE['lastVisit'];
echo "Your last visit was - ". $visit;
}
else
echo "You've got some stale cookies!";
?>
</body>
</html>

 

Suggested Read:

 

Results:
Initial state:

PHP code to display last visited time on the web page - initial state

 

First, revisit:

PHP code to display last visited time on the web page - after refresh

 

Second revisit:

PHP code to display last visited time on the web page - after second refresh




Author: Ravi Bandakkanavar

A Techie, Blogger, Web Designer, Programmer by passion who aspires to learn new Technologies every day. It has been 6 years since I have been publishing articles and enjoying every bit of it. I want to share knowledge and build a great community with people like you.

Suggested articles for you:

2 thoughts on “PHP program to display the last visited date-time

Did it help? Comment here..