InternetPower.Org

Connect - Communicate - Make a Difference

Writing PHP Code

This article gives an introduction to writing PHP code by describing how you can use PHP to show sections of HTML for defined periods of time.

This enables you to describe events in the future using HTML, with the information becoming invisible when the event happens.

You can also upload information to a website that will not be shown until a particular date, which is useful when information is not to be shown to the public until a known date. Examples include announcing that tickets are on sale for an event, or showing the content of a press release that has an embargo date.

To understand how to do this, the first thing to know is how the PHP date function works.

Details in the PHP Manual are here.

The key to success here is to get the date in the right format. The commonly used formats such as the UK short form dd/mm/yy (e.g. 01/04/08), or the textual Month Day Year (e.g. October 6th, 2008) do not work for programming purposes because you can't easily compare them to another date value to automatically decide if they are less than (before) or greater than (after) the other value.

There are two ways to make a date that can be reliably compared. One is to use Unix timestamps - these give the number of seconds after 00:00:00 on the 1st January 1970. They are very simple and can be manipulated as integer numbers having a strict sequence, incrementing by one every second, but they are not easy to read. Unix timestamps are machine-friendly but not human readable! So instead we can use dates in the format yyyymmdd which give an 8 character string whose characters are all digits. An example is 20080401 for 1st April 2008. These can be treated as integers and although they are not sequential they can be reliably compared.

To get the yyyymmdd version of the current date you do this (note the "Start PHP" and "End PHP" tags before and after the statement, which separate the PHP code from the HTML in which it is embedded:


<?php

       $today=date('Ymd');
       
?>

The clue to what is going on can be found at the above link to the PHP Manual's entry for the date function. The Y gets the year in 4 digit form, the m gets the month in 2 digit form with a leading zero, and the d does the same for the day.

Then all you have to do is to is compare that value to the fixed values of one or more reference dates and use the curly bracket conditional code syn tax to show something or not. Like this:

<?php

if ( ($today>'20080401') and ($today<'20080506') ) {

 echo '

Now that it is after the First of April 2008, we can announce that Everything is Happening on the 5th of May 2008!

'; } ?>

You can, to simplify things and avoid having to use the echo statement, exit from PHP inside the conditional part of the code, like this:

<?php

if ( ($today>'20080401') and ($today<'20080506') ) {
?>

<p class="headline">Now that it is after the First of April 2008, we can announce that
 Everything is Happening on the 5th of May 2008!</p>

<?php
}

?>

The piece of HTML will only be shown if the condition tested in the PHP code - the value of $today being greater than 20080401 and less than 20080506 - is satisfied.

A further simple use of PHP is to include pieces of HTML inside other pages. The time when almost any website needs this is to include a menu into each page. Updates to the menu then appear in all the pages without having to edit every page. There is a mechanism to do this using Server Side Includes (SSI) but sometimes these are disabled for security reasons. If you need to do this and cannot use SSI, or do not know how to, you can use PHP to achieve the same thing.

It is done with PHP Includes which work in a similar way:

<?php

include ('menu.php');

?>

The above will include the code in menu.php into the code of your PHP file. The code in menu.php does not need to be php though it is best to start the file with a php opening tag and then close that again after including a comment explaining what is happening:

<?php

/* Menu to be included in all pages of mywebsite.com */

?>

<a href="index.php">Home</a>
<a href="contact_us.php">Contact Us</a>
etc....

Website design and engineering by WessexCCS