Server Side Includes - SSI's:
Including Dates And Other Information From The Server
Overview
- E07a - (4:30)
It is very easy to display date information on your page using includes. The following in the code
<!--#echo var="DATE_LOCAL" -->
will return this information, which is not very well displayed:
Friday, 29-Mar-2024 02:38:28 MST
Below is a table that shows you the code (argument) needed to have the server provide you with one or more parts of the date. Note that the date is based on the location of the server not local time. The argument needs to be put into other code, which will be explained further below:
Argument | What it will display | Results of the include code |
---|---|---|
%a |
Short name of day | Fri |
%A |
Full name of day | Friday |
%b |
Short name of month | Mar |
%B |
Full month name | March |
%m |
Month number | 03 |
%d |
Number of day in the month | 29 |
%x |
Date in standard format | 03/29/24 |
%y |
Two-digit year number | 24 |
%Y |
Four-digit year | 2024 |
%H |
Hour Number (24-Hour Clock) | 02 |
%I |
Hour Number (12-Hour Clock) | 02 |
%p |
AM or PM | AM |
%M |
Minute Number | 03 |
%Z |
Time Zone | MST |
In the table above:
- The Argument column shows different options that will extract from the server's "DATE_LOCAL" variable the server's local date, day, month, time, etc.
- The column What it will display describes the result.
- The right column shows the actual results of using that argument in the context shown below (that is, with the two lines of code, the config and echo lines).
The following example code below uses the %x argument which, as you will see in the table above, provides the date in what it calls standard format. It follows here: 03/29/24 . The word echo in this context instructs the server to echo back the current status of the variable "DATE_LOCAL", which is the server's local time and date. The argument specifies what part of that date information to display:
<!--#config timefmt="%x" -->
<!--#echo var="DATE_LOCAL" -->
You can assemble more than one of the arguments shown in the table above as is seen here:
Combining several arguments | |
---|---|
The code in the left cell below is used to display the month, day, and year. The result is shown in the cell below on the right. Notice that there is a comma to properly punctuate the information: |
|
<!--#config timefmt="%b %d, %Y"--> |
Results of the code on left: Mar 29, 2024 |
Assignment:
NOTE! The results of your include exercises will be posted here (as soon as I grade them).
- E07b - (5:14)
- Create a new file naming it: yourlastname_E07.shtml
- Be sure to include the "s" before the "html" to get .shtml
- Add this HTML title: yourlastname E07
- Put your last name inside of h3 tags just beneath the opening <body> tag.
- Using the example code directly above as your guide, assemble code that will result in the following, all in one row, in this order:
- Full name of day followed by a comma ( , )
- Full name of month
- Day of month immediately followed by a comma ( , )
- Full year
- Put that code inside an html comment with the code for the config and timefmt as per the example in the table above. Note that your code after the timefmt will look somewhat different.
- Be sure that there is one pair of quotes surrounding all the time format symbols, as you see in the examples above.
- Add this in a separate line beneath the config line:
<!--#echo var="DATE_LOCAL" -->
- Your code ought to look something like the code in the table above, but with different code after timefmt.
- I will upload the results of your code with your first name once I receive it (if I forget, kindly remind me!).
For more information about Server Side Include dates:
In the table below are other types of information retrieved from the server using variables. The variables in the left column need to be put into the echo include's var
attribute as in this example which used the first variable in the table:
<!--#echo var="HTTP_USER_AGENT" -->
Also note that the variables that result in a time have a line in the code before the echo line such as the following. This code displays the time in hours based on a 12-hour clock, minutes, AM/PM. Then the date is displayed using the code shown above in the table "Combining several arguments"):
<!--#config timefmt="%I:%M %p, %b %d, %Y"-->
Variable Name | Result | Description |
---|---|---|
HTTP_USER_AGENT | claudebot | The browser you are using, with other system information. |
DATE_GMT | 09:38 AM, Mar 29, 2024 | Current date and time in GMT (Greenwich Mean Time) |
DATE_LOCAL | 02:38 AM, Mar 29, 2024 | Current date and time in the local time zone |
DOCUMENT_NAME | includedDates.shtml | The current file |
HTTP_HOST | www.gottheknack.com | The web site that the browser requested a page from. |
LAST_MODIFIED | 08:28 PM, Jun 22, 2015 | Last modification date and time for current file |
SERVER_SOFTWARE | Apache | The name and version of the server software that is answering the client request. |
Note that JavaScript can also be used to insert the time, date, etc., but that JavaScript code would be executed by the browser in order to get the time, with ssi it's the server that does that work.