Friday, March 29, 2024

SSI: Dates and Times

Use these to jump around or read it all…


[Config and Echo]

[The timefmt Argument]

[The Codes]

[Multiple Codes]


This is the second in a series of three tutorials on SSIs (Server Side Includes). These are commands from the PERL language that allow you to include files and information right from the server.

If you haven’t already, please take the time to read the original SSI tutorial first.

In that tutorial you’ll get a little script that allows you to test your server to see if you’re even able to play with these commands, and a lesson on the “include” command. It’s best that you do read that first because I’m going to assume you have at this point and just bull forward.

Bull, bull, bull…


Config and Echo

Let’s talk about the basic format first. Once we have that, then the rest is a simple chart of what argument returns what. Here’s the format:

<!–#config timefmt=”–” –> <!–#echo var=”DATE_LOCAL” –>

The format for these includes is quite rigid. You must keep it the same. Do not put in spaces where they don’t now appear. HTML is flexible. This is not. The biggest mistake I make is not having a space just before the final two dashes at the end of the command lines. Make sure that’s in there. You don’t get error warnings with these commands. They simply fail to work and you can spend hours trying to figure out what’s wrong when it’s only a missing space.

I guess the point I’m making is to not mess with the layout of the command line.

That said, look at what we have here. It’s actually two command lines, one after the other. The first command line denotes the time format and the second echoes it back. See that?

Let me start with the second one because that’s the one that requires the least amount of work. In fact, it requires no work. Leave it the way it is. The echo command should always have the var argument set to “DATE_LOCAL”, in caps, with an underscore between the words.

Every time you set an include to grab a date or a time, always follow that command with this echo command line so it echoes back and gets posted to the page.

I think I’ve driven that point into the ground.


The timefmt Argument

Moving backward, we get to the first command line. That’s the one that reads #config timefmt=”–“.

The leading hash mark (#) is required, keep it. The argument “timefmt=” is used every time you want to return a date or time. The big question is what you want returned. There are 21 choices, each identified by a percent mark, a one-letter code that goes in between the quotes following “timefmt=”.


The Codes

The codes make little sense to me. They don’t seem to represent what they return, so you’ll need some kind of chart to keep it all straight. I have one below.

I’ll use the first code, %a, to demonstrate a command line. The code %a will return the abbreviated weekday name (Mon, Tue, Wed) depending on the current local. The command would look like this:

<!–#config timefmt=”%a” –> <!–#echo var=”DATE_LOCAL” –>

     …and here’s what it gives you:


There’s no need looking at the source code to see how I placed the command lines because they won’t be there. Only the return provided by the server will display.

Here are all 21 timefmt arguments, what you’ll get, and an example.

Argument You Get: It Looks Like:
%a Abbreviated Weekday Name
%A Full Weekday Name
%b Abbreviated Month Name
%B Full Month Name
%c Preferred Date & Time
%d Day of Month as digit
%H Hour Number (24-Hour Clock)
%I Hour Number (12-Hour Clock)
%j Day of the Year Number
%m Month as digit
%M Minute Number
%p AM or PM
%S Second Number
%U Week Number/Sunday as Day One
%w Day of the Week Number
%x Preferred Format without Time
%X Preferred Format without Date
%y Two-Digit Year Number
%Y Four-Digit Year
%Z Time Zone

You might notice that some of the elements claim they represent the “preferred” format; “preferred” means the default settings of the server. It can differ from server to server.


Notice also that these returns are from the server. If you are in one time zone and the server is in another, the time zone the server is in will be reflected in the returned values.


Multiple Codes

The codes above can be used one right after another to create a longer date/time stamp. Let’s say you want something that looks like this:

Wednesday, January 26, 2000

You could set up an SSI that looks like this:

<!–#config timefmt=”%A, %B %d, %Y” –> <!–#echo var=”DATE_LOCAL” –>

See how one just followed the other? The commas are in there, too. You can make up any combination you want by just following one with the other, leaving spaces and putting commas where you want them. Very cool.


That’s That

These are great includes. My school Webmaster uses them to death. Hopefully, you too can find a use for them.

Keep in mind that I also have the original SSI include tutorial and the SSI tutorial on file returns.

 


Enjoy!

 


[Config and Echo]

[The timefmt Argument]

[The Codes]

[Multiple Codes]

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured