PHP: Hot Pages: Basic Code: Dates and Times

  1. Basic Code
  2. Functions

image 2PHP code starts looking like HTML code and ends looking nothing like it. If you’ve looked at HTML code, you’ve seen things that look like “<em>” or “<h2>”. Here’s a simple web page:

<!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>
<p>The current time is 9:34 AM.</p>



You can see this file in the resources archive as “time.php”. Go ahead and grab it, and save it on your web site. Of course, even by the time you first view this page the current time is probably going to be wrong, and for your visitors it will be right only one minute out of every day. This is a great example of web content that would be better served with PHP. You will put your PHP code between “<?php” and “?>”. Here’s how to display the current time:

<p>The current time is <?php echo date('h:i A')?>.</p>

If you save this file as “time.php” on your web site, and then view it, you will see the current time every time you reload your page.

  1. Basic Code
  2. Functions