About the Author

author photo

Adam Trachtenberg is the Director of Platform and Services at eBay. Before eBay, Adam co-founded and served as vice president for development at two companies, Student.Com and TVGrid.Com. Adam is the author of Upgrading to PHP 5 and coauthor of PHP Cookbook. He lives in San Francisco, California.

See All Posts by This Author

XSLT Cookbook: Generating an RFC 822 Date

Problem
You want to generate an RFC 822-formatted date in XSLT. This is needed, for example, in RSS feeds.

Solution
Use this code:


<xsl:value-of select="concat(date:day-abbreviation(), ', ',
  format-number(date:day-in-month(), '00'), ' ',
  date:month-abbreviation(), ' ', date:year(), ' ',
  format-number(date:hour-in-day(), '00'), ':',
  format-number(date:minute-in-hour(), '00'), ':',
  format-number(date:second-in-minute(), '00'), ' GMT')"/>

This assumes your server is in GMT.

Discussion
XSL uses a different default date format style than RSS, so it’s a bit of a pain to generate the current date in the RFC 822 format.

This code requires the use of the EXSLT date functions. Add them to your stylesheet by modifying the top to look like this:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:date="http://exslt.org/dates-and-times"
                extension-element-prefixes=date"
                version="1.0">

I use the format-number() function to make sure all numbers have leading 0s. The date functions return 3 instead of 03, for example.

Popularity: 7% [?]

Post a Response