WebThings - by Jan Weijers
All chapters...
Table of contents
A simple HTML document
Cascading style sheets
Programming in PHP
Server side includes
CGI & Perl
Extended characters
Examples of colours
Learn more
Recent changes
Alphabetical index
In this chapter...
Embedding PHP
Printing date and time
PHP is multilingual
Date of the current page
Date of another page
Date dependent output
Processing forms



U hebt gekozen om Engels als eerste taal te gebruiken.Nederlands vlaggetje Klik hier om te kiezen voor Nederlands.

Programming in PHP

PHP, which stands for "PHP: Hypertext Preprocessor," is a widely-used Open Source general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. Full documentation of PHP can be found at www.php.net

Embedding PHP

Including PHP in your webpages is simple. One most webservers, you will need to give your file and .php extension rather than a .html one. That will tell the webserver to process the file with PHP. You can mix PHP and HTML like this:

The time is: <?php echo time() ?> seconds since 1970...

The time is: 1597279202 seconds since 1970...

The time you see now is the Unix time stamp, seconds since 1970. Longer code can be mixed with HTML in this way:

The table of seven:<BR>

   for ( $i=1; $i<11; $i++) {
      print $i . " x 7 = " . $i * 7 . "<BR>";


The table of seven:
1 x 7 = 7
2 x 7 = 14
3 x 7 = 21
4 x 7 = 28
5 x 7 = 35
6 x 7 = 42
7 x 7 = 49
8 x 7 = 56
9 x 7 = 63
10 x 7 = 70

<?php tells PHP that code starts there, ?> indicates the end of code. In the first example you see the use of time(), one of the many build-in functions of PHP. This one gives the time in seconds since 1970, the Unix timestamp. The echo and print functions do essentially the same, they output to the HTML page. The second example shows the use of a variable names $i.

Every line in a PHP script ends with a semicolon, unless there is only one line in a script. A viewer of your page cannot see anything that is between <?php and ?>, also not when using the "view source" option in a browser. Only the output of a print or echo function will appear in a web page.

To the top

Printing date and time

PHP is very useful to make basic dynamic elements in your page such as printing the date and the time:

Welcome, today is <?php echo strftime("%A") ?> and the time is <?php echo strftime("%R") ?>.

Welcome, today is Thursday and the time is 02:40.

The strftime() function produces dates and times. It takes at least one argument, a string to format the date. You have many options, here are some of the most important ones:

%aabbreviated weekday name
%Afull weekday name
%babbreviated month name
%Bfull month name
%cpreferred date and time representation
%dday of the month as a decimal number (range 01 to 31)
%eday of the month as a decimal number (range ' 1' to '31')
%Hhour as a decimal number using a 24-hour clock (range 00 to 23)
%Ihour as a decimal number using a 12-hour clock (range 01 to 12)
%mmonth as a decimal number (range 01 to 12)
%Mminute as a decimal number
%peither `am' or `pm' according to the given time value
%rtime in a.m. and p.m. notation
%Rtime in 24 hour notation
%Ssecond as a decimal number
%Tcurrent time, equal to %H:%M:%S
%uweekday as a decimal number
%Uweek number of the current year as a decimal number
%xpreferred date representation
%Xpreferred time representation
%yyear as a decimal number without a century (range 00 to 99)
%Yyear as a decimal number including the century
%Z or %z   time zone or name or abbreviation

You can combine as many of these in one strftime() command as you wish:

<?php print "Today is: " . strftime("%a, %e %B '%y and the time is %H:%M:%S") ?>

Today is: Thu, 13 August '20 and the time is 02:40:02

The strftime() function normally outputs the time at the moment it is called. You can give the strftime() function a second argument, the time in seconds since January 1, 1970. This is the standard Unix way of describing the time. For example, December 13, 2003 is 1071270000:

<?php print "Another date was " . strftime("%A, %d %B %Y", 1071270000) ?>

Another date was Saturday, 13 December 2003

To the top

PHP is multilingual

PHP knows the names of days and months in many languages. You can use the set_locale() function to chose one:

   setlocale(LC_ALL, "nl_NL");
   print strftime ("%A %d %B") . "<BR>";
   setlocale(LC_ALL, "fr_FR");
   print strftime ("%A %d %B");

Thursday 13 August
Thursday 13 August

This is especially practical if your site is multilingual.

To the top

Date of the current page

PHP provides a handy function to get the modification date of the current page. The getlastmod() function returns a Unix timestamp which you can feed to the strftime() function:

<?php print "This page last updated: " . strftime ("%A %d %B %Y", getlastmod()) ?>

This page last updated: Saturday 02 June 2012

Remember that once you upload your file by FTP to a web server, you are like to see the date you last uploaded the file, not the date you actually modified it.

To the top

Date of another page

To get the modification date of another file, you need to do some more work:

   $filename = 'index.php';
   if (file_exists($filename)) {
    echo "$filename was last modified: " . strftime("%A %d %B %Y", filemtime($filename));

index.php was last modified: Saturday 02 June 2012

This small script first uses the file_exists() function to verify the file actually exists. If the file exists, the filemtime() function is called to get the date the file was last modified. You can only use these filesystems function for information on files that are on the same server as your script.

To the top

Date dependent output

Using the date and time functions of PHP you can make parts of your web pages dependent on de date. Consider these two examples:

   if ( strftime("%H") < 12 ) {
      echo "Goodmorning!";
   elseif ( strftime("%H") < 18 ) {
      echo "Good afternoon";
   else {
      echo "Good evening";


   if ( strftime("%m") == 12 ) {
      echo "December offer: 5 for the price of 6";
   else {
      echo "Special offer: 6 for the price of 5";

Special offer: 6 for the price of 5

In both examples you see the use of the "if, elseif, else" control structure. If and elseif are followed by a logical expression. Else is what happens if none of the logical expressions is true. You can use as many "elseif" structures as you want to.

To the top

Processing forms

In the chapter on forms I use a simple script that processes the output of all the forms and prints them into a table. This is how the script looks:

<HTML><HEAD><TITLE>Form output tester</TITLE></HEAD><BODY>

<TR><TD><B>Name / Naam</B></TD><TD><B>Value / Waarde</B></TD></TR>

   foreach ( $_POST as $i => $j ) {
      $i =substr(strip_tags($i), 0, 20);
      $j =substr(strip_tags($j), 0, 255);
      // print each line
      print "<TR><TD>$i</TD><TD>$j</TD></TR>";


<P><A HREF="" onClick="self.close()">Close window / Sluit venster</A></P>


The script makes use of the predefined variable $_POST. $_POST is an array. If a form uses the METHOD="post" attribute, all input from the form will be in the $_POST array. For example, if you have a text field named "email" than that input can be found in $_POST['email'].

Because $_POST is an array, you can use the foreach control structure to get the contents of the array. foreach takes every value in $_POST and, in the example above, stores the name in $i and the value in $j. This technique makes the script very flexible and enables it to capture the input from any form, irrespective of the number of fields it has and what they are called.

The script uses two build-in functions of PHP to for security. The strip_tags() function removes all tags, PHP and HTML. This avoids malicious users from including scripts or risky tags in the form. A second security measure is to maximize the length of the input. This is done with the substr() function. This function takes three arguments. The first is a string, the second is the starting point and the third is the lenght of the string to take. For the starting point 0 means the first character. So $i is cut to a maximum length of 20 characters and $j to a maximum of 255 characters. As you can see from this example you can nest functions without problems.

Finally, the script uses the print function. You can see how the two variables $i and $j can be included in a string and will print properly.

This is a very basic example of processing a form. In real life you will want to validate the input, for example check if all obligatory fields have been filled out. Then you want to do something with the input, like e-mail it to yourself of store it in a database.

To the top


Feedback welcome...

Please let me know your opinion about this guide, information you would like to be added or any errors you have discovered. Click here.


Open standards...

This site conforms completely to open standards of the World Wide Web Consortium (W3C). Click on the logos to verify that.

HTML 4 logo

CSS logo



©1995-2020 Jan Weijers, the Netherlands. All rights reserved.
No reproduction by any means or in any language without prior permission of the author.