Sam Brown

Customising the Date Format in Textpattern

With the release of my new design I wanted to use a short date format for my entries, however the default Textpattern install while having many different options didn’t have the exact one I wanted. A little hacking required but it was an easy fix, here is how I accomplished it.

If you open the /textpattern/includes/txp_prefs.php file you will find on or around line 354 the dateformats() function. In the $formats array you can edit or delete any of the current options, I decided to create a couple of my own and added them to the top of the array:

  1. "$daynumord $monthshort $yearshort",
  2. "$daynumord $monthshort $yearshort $time24",
  3. Download this code: /code/txp_dateformat.txt

These are pretty self explanatory, the first displays the day, a short version of the month and a short version of the year. The second line displays the same thing except it adds on the time in 24 hour format to the end (for my archive).

That is all you need to do, save and reupload the file and now when you visit Admin > Preferences within Textpattern you will see your new options.

The only issue with hacking the file is that when you next update Textpattern your changes will be overwritten. Currently there is no decent plugin to accomplish this, hopefully there will be in the future so we do not have to hack the core Textpattern files.

Post a link to this on Twitter ↩


qrayg 18 September 2007, 08:10 #1

Is there a reason why you couldn’t just use the format attribute in < txp:posted > and use the strftime() in PHP to format the time?

Sam Brown 18 September 2007, 08:42 #2

I had tried that method previously but was having trouble getting the exact values I wanted. This is actually a hack I have been using for a couple of years probably.

For instance if you want different date formats on the article listing, individual article and comments you have to create a couple of if statements. This way I can just select the ones I want.

Granted, editing the <txp:posted /> format attribute is the easiest and most future-proof option. I may go back and actually use this method, thanks for reminding me.

Commenting has closed for this article. Feel free to me.

Sam Brown co-founded Iterate, and was previously VP of Design at Foursquare. Based in NYC.