About the Author

author photo

Adam Trachtenberg is the Senior Manager of Platform Evangelism at eBay, where he preaches the gospel of the eBay platform to developers and businessmen around the globe. Before eBay, Adam co-founded and served as vice president for development at two companies, Student.Com and TVGrid.Com. At both firms, he led the front- and middle-end web site design and development. Adam began using PHP in 1997, and is the author of Upgrading to PHP 5 and coauthor of PHP Cookbook, both published by O'Reilly Media. He lives in San Francisco, California, and has a B.A. and M.B.A. from Columbia University.

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.

Post a Response