etc_date

2013-03-26 21:21

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 2013-03-26 21:21, 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 2021-12-01 15:38, the current server date.
  • <txp:etc_date date="next Friday" /> outputs 2021-12-03 00:00.
  • <txp:etc_date date="posted" /> outputs 2013-03-26 21:21, this article’s publish date.
  • <txp:etc_date date="posted" lang="fr_FR" /> outputs 2013-03-26 21:21, 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 25 Rabiulakhir 1443.
  • <txp:etc_date calendar="jalali" lang="fa_IR" format="%d %B %Y" /> outputs 1400 سپتامبر 10.

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 23 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 3171 days and 18 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)