I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter or add me on Google+.

web developer guide

my books

Book cover of Pro CSS3 AnimationPro CSS3 Animation, Apress, 2013

my other blogs

Massive Head Canon: Intelligent discussion of movies, books, games, and technology.

my projects

A Sass color keyword system for designers. Replaces CSS defaults with improved hues and more memorable, relevant color names.

CSSslidy: an auto-generated #RWD image slider. 3.8K of JS, no JQuery. Drop in images, add a line of CSS. Done.

tipster.ioAutomatically provides local tipping customs and percentages for services anywhere.

The PHP date function

php / functions

Estimated reading time: 3 minutes, 30 seconds

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, and 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'); ?>

(That’s a lowercase L used inside single quotes for the date function.)

This piece of code demonstrates several important points:

  1. 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.

  2. echo can be used to print out strings, the value of variables, or the result of functions.

  3. It is important that there is a space somewhere between “is” and the day of the week, otherwise the text will run together.

  4. 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 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'); ?>

    Using concatenation, the result is a much more verbose readout of the current day and time : “It is now 3:07 am on Monday 31 December 2014”.

    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 , 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 &copy; 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 &copy; <?php echo date('Y'); ?> Grievous Genophage Inc.</h4>
comments powered by Disqus

This site helps millions of visitors while remaining ad-free. For less than the price of a cup of coffee, you can help pay for bandwidth and server costs while encouraging further articles.