Display the Date with PHP

Learn how to put a little kick in your static website and display the date via PHP.

You would be surprised at how many simple things you can do to make managing your static website that much easier. In this tutorial I will show you guys how to use some basic PHP to display and customize the date that is displayed on a web page on your site.

1. PHP has a built in date function that essentially formats a timestamp. Make sure your HTML document has the “.php” extension to ensure the code will execute.

2. The PHP Date() function looks like this:

date(format,timestamp)

The arguments:
format: Is required and controls the display for the timestamp
timestamp: Is not required and is simply an integer Unix timestamp

Let’s say we want our date to appear in-between two paragraph tags.

<p>Date Here</p>

3. We will construct our php Data() function like so:

<?php date("m/d/Y"); ?>

The arguments for the date format are the following:

m: Displays the month from 01 – 12
d: Displays the day of the month from 01 to 31
Y: Displays the year in 4 digits

4. Now we simply take our PHP code and place in in our HTML between our paragraph tags:

<p><?php date("m/d/Y"); ?></p>

You will see the date formatted as the following:

02/12/1991

Using these steps, you have have your website dynamically change the date depending on the day. Keep in mind, you can use different arguments to display the date differently. For example, if you simply used a capital M, the month would be displayed as an abbreviation in text such as JAN.

For a full list of the different arguments to format your date dunction check out: http://php.net/manual/en/function.date.php

 

About Chris Luongo

Chris is a self-taught web designer and developer out of Atlanta, GA. This geek enjoys coffee, cold brews, bike riding, and twisting any form of tech into submission.
View more articles by Chris Luongo

The Conversation

Follow the reactions below and share your own thoughts.