About the Author

author photo

Adam Trachtenberg is the Director of the LinkedIn Developer Network, where he oversees developer relations and marketing for the LinkedIn Platform. Before LinkedIn, Adam worked at eBay in platform product management and marketing. Even earlier, he co-founded Student.Com and TVGrid.com. Adam is the author of PHP Cookbook and Upgrading to PHP 5. He lives in San Francisco.

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