PHP: Hot Pages: Basic Code: The Final Code

  1. Avoiding errors
  2. Basic Code

Here is the final code we used for this section:

<?php

ini_set('display_errors', true);

error_reporting(E_ALL);

session_start();

?>

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Time for PHP</title>

<style>

body {

margin: 10em;

padding: 2em;

border: solid .2em green;

}

h1 {

text-align: center;

}

</style>

</head>

<body>

<h1>PHP Time</h1>

<?php

//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>

</body>

</html>

  1. Avoiding errors
  2. Basic Code