SSI Echo
In addition to including files into your page, Server Side Includes can also be used to get information from the server and write it to the page before it is sent to the reader. Though they aren’t meant as replacements for more sophisticated methods of generating dynamic content such as CGI or PHP, SSI are a nice way of adding small details to your page. Here we will discuss the file, date and time options.
This page was last updated on 2012-08-21
Config and Echo
If you have read through the introductory tutorial on file Includes, you should now be familiar with the format of an SSI directive. They generally look like this:
<!--#command argument="value" -->
We’re about to use two new ones to print the date to the page. These are config
and echo
— config
sets the format that one of the SSI variables will be returned in (we’re gonna print the date so we’ll be using the local date variable, DATE_LOCAL
), and echo
writes one of the variables into the file.
The time returned is the time on the server. This will most likely be different from the time on your computer and your visitor’s, since they may not be in the same time zone. If you want to print the time it is on the local computer, use the JavaScript date
object.
To write the current date and/or time to a .shtml
file, add this line wherever you want it to show up:
<!--#config timefmt="%c" --><!--#echo var="DATE_LOCAL" -->
This will return something like
Sun Jan 25 12:33:47 2004
Timestamp Format Codes
Above we used the %c
value to return a full date and time, but there are 19 other choices for how you want the timestamp to look. The values you can give timefmt
are all single characters, though you’ll need a chart to remember them all.
Value | Description | Example |
---|---|---|
%a |
Abbreviated weekday name | Sun |
%A |
Full weekday name | Sunday |
%b |
Abbreviated month name | Feb |
%B |
Full month name | February |
%c |
Standard date & time | Mon Oct 22 11:21:55 2004 |
%d |
Day of month as digit | 12 |
%H |
Hour number (24-hour clock) | 22 |
%I |
Hour number (12-hour clock) | 10 |
%j |
Day of the year as digit | 047 |
%m |
Month as digit | 06 |
%M |
Minute number | 02 |
%p |
AM or PM | PM |
%S |
Second number | 1071178029 |
%U |
Week number | 05 |
%w |
Day of the week number (with Sunday as first day) | 1 |
%x |
Date in standard format | 01/12/05 |
%X |
Time in standard format | 12:04:45 |
%y |
Two-digit year number | 04 |
%Y |
Four-digit year | 2004 |
%Z |
Time zone | EST |
When the output is referred to as “standard” above, it means that the server will return it’s normal format for that value. This may be different from server to server.
Advanced Formatting
All these options are nice, but what you’ll probably end up doing is combining some of them to create your preferred format. For instance, to create a date like Tuesday 11 November, 2004, you would write
<!--#config timefmt="%A %d %B, %Y" -->
<!--#echo var="DATE_LOCAL" -->
Just whack them all in there together, along with whatever punctuation you need.
Printing File Data
To print out the date and time when a file was last modified, use the flastmod
command. This will return a date and time, the format of which is specified in the last timefmt
directive you wrote. An example, and its output:
<!--#config timefmt="%d/%m/%Y" -->
Last modified on <!--#flastmod virtual="/index.shtml" -->
Last modified on 25/01/2004
You can also print out the filesize of a file with fsize
. The format of the returned value depends on the directive sizefmt
, which can be set to bytes
for a formatted byte-count (eg: 23,720), or the default, abbrev
, for an abbreviated count rounded up into kilobytes or megabytes.
<!--#config sizefmt="bytes" -->
Filesize: <!--#fsize virtual="/index.shtml" -->
SSI Environment Variables
In addition to the CGI environment variables, SSIs also have access to a number of built-in values which can be echoed to the page. These are:
DOCUMENT_NAME
- The current filename.
DOCUMENT_URI
- The virtual path to this document (such as
/stylesheets/index.shtml
). QUERY_STRING_UNESCAPED
- The unescaped version of any search query the client sent, with all characters special to the shell escaped with a backslash.
DATE_LOCAL
- The current date in the server’s local time zone, as seen above.
DATE_GMT
- Same as
DATE_LOCAL
but in Greenwich mean time. LAST_MODIFIED
- The last modification date of the current document. Subject to
timefmt
like the other dates.
These are all accessed in the same way,
<!--#echo var="DOCUMENT_NAME" -->