Year 4.7

2 November 2018, 00:24

Keywords: date, time, calendar

Among other enhancements, Textpattern 4.7 has greatly simplified dates processing. To give an example, let’s cite Destry’s request on the Forum:

In the old Parisian journals (newspapers) of the Belle Époque it was common to keep track of paper issues by using Nth année — No. N+1

The first part is counting the year from the first year the paper was published. A given issue would be 1er année in the first year, 2e année in the second, and similar thereon. […] For example, if a journal launched in 2016, it would now be 2e année

The first article on this site was published the 03 Jul 2012, so today (i.e. 02 Nov 2018) is the 7th year from the beginning. How can we calculate it?

The math behind it is very simple: divide the difference in seconds between today and 03 Jul 2012 by the number of seconds in an average year and round up the result to the nearest integer. Here are the concerned values:

  • The UNIX timestamp of 03 Jul 2012 obtained via strtotime('2012-07-03') PHP function is 1341266400.
  • The UNIX timestamp of this article’s publication date is given by <txp:posted format="%s" />.
  • The number of seconds in an average year is 365.25*24*60*60 = 31557600.

It remains to plug this data in <txp:evaluate />

<txp:evaluate query='ceiling((<txp:posted format="%s" /> - 1341266400) div 31557600)' />th year

to get 7th year.

Easy, isn’t it… but not very accurate, because of leap years. The rigorous solution is elementary as well:

<txp:evaluate query='ceiling((<txp:posted format="%Y%m%d" /> - 20120702) div 10000)' />

and gives 7, as expected.

And if you need to output all articles of, say, the 3rd year, use month and time attributes of <txp:article_custom />:

<txp:article_custom month="2012-07-03 +2 years" time="+1 year" />
Video embeds (9 September 2014, 15:36) , Dynamic menu (26 September 2014, 13:03) , etc_tree (5 October 2014, 16:29) , Retain form values (21 November 2014, 23:04) , Infinite scroll (13 March 2015, 11:59)