etc_date

26 March 2013

Keywords: date,time,locale,calendar

Textpattern has localized strings for almost everything, but dates are under servers responsibility. This is reasonable, since dates are more than just months/days names, but can be a problem if some locales are not installed on the server. For example, even if I switch Textpattern current language to Russian, <txp:posted /> will output 26 March 2013, since ru_RU locale is not available on our server.

This is where etc_date intervenes. It allows you to define your own date strings to display dates in the language of your choice. Moreover, you can use custom calendars, to transform 1799-11-09 into 18 Brumaire An VIII (though currently only Gregorian, Hijri and Jalali calendars are available).

Even if you do not need to localize dates, etc_date can come handy, since it understands expressions like today, next Friday, etc.

Enough words, place to the action.

  • <txp:etc_date /> outputs 21 November 2017, the current server date.
  • <txp:etc_date date="next Friday" /> outputs 24 November 2017.
  • <txp:etc_date date="posted" /> outputs 26 March 2013, this article’s publish date.
  • <txp:etc_date date="posted" lang="fr_FR" /> outputs 26 mars 2013, since fr_FR locale is available on this site.
  • <txp:etc_date date="posted" lang="ru_RU" format="%A %d %B %Y" /> outputs Вторник 26 марта 2013. This time, ru_RU is unavailable, but month and weekday names are defined in plugin preferences.
  • <txp:etc_date calendar="hijri" lang="ar_AR" format="%d %B %Y" /> outputs 2 Rabiulawal 1439.
  • <txp:etc_date calendar="jalali" lang="fa_IR" format="%d %B %Y" /> outputs 1396 اوت 30.

Date ranges

<txp:etc_date date="sunday..sunday +6 days" wraptag="blockquote" break=", ">
	<txp:etc_date timestamp='<txp:etc_timestamp />' format="%A" /> -
	<txp:etc_date timestamp='<txp:etc_timestamp />' format="%A" lang="ru_RU" />
</txp:etc_date>
Sunday - Воскресенье , Monday - Понедельник , Tuesday - Вторник , Wednesday - Среда , Thursday - Четверг , Friday - Пятница , Saturday - Суббота

Dates comparison:

<txp:etc_if_date value="12-24..12-31" format="%m-%d">
	Merry Christmas and Happy New Year!
<txp:else />
	Yet <txp:etc_date_diff from="now" to="December 25" format="%a" /> days until Christmas...
</txp:etc_if_date>
Yet 33 days until Christmas...
<txp:etc_if_date date="expires" compare="now..+3 days" format="%s">
	This article expires in less than three days!
</txp:etc_if_date>

According to <txp:etc_date_diff from="posted" to="now" format="%a days and %h hours" />, this very article is published 1700 days and 3 hours ago.

Localized date strings

Localized date strings are not supplied with the plugin, but here are some links kindly posted by our users:

File(s)