After include(), date() is the first function that developers respond to with “Oh, that’s why I would use PHP!”
Before we get to the function itself, a small reminder: on the whole, HTML and CSS are stateless, which means (among other things) that they have no concept of time. The only way to place today’s date on a webpage using HTML is to physically write it out on the page, mark up the code, and upload it to the server… each and every day.
With PHP, placing the current day of the week on the page couldn’t be easier:
- <p>Today is <?php echo date(‘l’); ?></p>
(That’s a lowercase L used inside single quotes for the date function.)
This piece of code demonstrates several important points:
PHP is, generally speaking, used for dynamic data, i.e. content that changes. In this example, we always want our paragraph to start with the words “Today is”, so it makes little sense to place those words inside a PHP statement. The only thing that changes is the actual name of the day, so that part is PHP.
echocan be used to print out strings, the value of variables, or the result of functions.It is still important that the
<p>tag be closed, and that there is a space somewhere between the word “is” and the day of the week.
The date() function works with “switches”: individual letters placed into the function to generate date components. date() can also be used to generate the time. A complete list of switches can be found at the php.net website entry for date(), but for now let’s use an example:
- <p>It is now <?php echo date('g:i A')." on ".date('l j F, Y'); ?></p>
This will give us a much more verbose readout of the current day and time, for example : “It is now 3:07 am on Monday 31 December 2011”. (We will cover the use of the concatenation period used in the code in a later lesson.)
Answers
PHP is a server-side scripting language, so by default it reports the local date and time of the server. This is actually a good thing: servers are at a known place, and keep regular, dependable track of time, whereas clients can and will have any time they wish… which will sometimes be erroneous.
In order to update the time and date we would have to physically refresh the page. PHP is server-side: it only works after communication with the server. By refreshing the page we are doing the equivalent of sending the dumbwaiter used in our earlier analogy back downstairs with an order to “give me something new”. It would be massive overkill to have to do this every minute in order to have a counting clock on the page: if you wanted a ticking clock on a web page it would make much more sense to write it in JavaScript, which works client-side.
Using date() with includes
The date() function can also be used in includes. Oddly, you do not have to change the extension of the include file to do so. (This means that any PHP that you use on the included file won’t be color-coded correctly in programs like Dreamweaver, but that’s not a major issue. You don’t have to change the extension as any PHP in an included file is run after it is called into a PHP page.)
For example, let’s say you have a copyright notice at the bottom of every page of your website. Having thought ahead, you have decided to make this portion of code an include file, with the filename footer.html. In this file is the following code:
- <h4>Copyright © 2010 Grievous Genophage Inc.</h4>
Your client is copyright-obsessed, and wants the copyright notice updated every year. Rather than using New Year’s to update the website every time, you can modify footer.html to become:
- <h4>Copyright © <?php echo date(‘Y’); ?> Grievous Genophage Inc.</h4>
so we don't need the jQuery minimum length anymore:) cool!


