PHP: Hot Pages: Basic Code: The Final Code

  1. Avoiding errors
  2. Basic Code

Here is the final code we used for this section:


ini_set('display_errors', true);




<!doctype html>

<html lang="en">


<meta charset="utf-8">

<title>Time for PHP</title>


body {

margin: 10em;

padding: 2em;

border: solid .2em green;


h1 {

text-align: center;





<h1>PHP Time</h1>


//formatted current time, with seconds for impatient visitors

function currentTime() {

$format = 'h:i A';

if (isset($_SESSION['lastVisit'])) {

$lastVisit = $_SESSION['lastVisit'];

if ($lastVisit > time()-60) {

$format = 'h:i:s A';



$_SESSION['lastVisit'] = time();

return date($format);


$now = currentTime();


<p><em>Right now</em> is <?php echo $now; ?>. <?php echo $now; ?> is an important time, because <?php echo $now; ?> is the exact time you visited our wonderful page.</p>



  1. Avoiding errors
  2. Basic Code