Managing Dates

SPIP contains a series of criteria and tags for managing the dates of articles.

Displaying dates

-   #DATE is the date of online publication. It can be changed after the publication of an article or of a news item etc. The date of a section is that of the most recent item contained in the section.

-  #DATE_REDAC is the “date of earlier publishing”. It is available only for articles and can be changed as much as you like.

-  #DATE_MODIF returns the date the article was last modified.

-  #DATE_NOUVEAUTES allows you to display the date of the last sending of the mail presenting the news.

Formatting dates

If no filter is used, the #DATE... tags are displayed in native MySQL format: "2001-12-01 03:25:02".

The filters: |annee, |mois, |jour, |heures, |minutes, |secondes, mais aussi |affdate, |date_relative, |nom_mois, |nom_jour, |saison, etc. can be applied to offer various different display formats. A complete list of filters which can be used to format the display of date tags is supplied in the article SPIP’s Filters.

Date context

SPIP provides all loops with a date context. If you’re inside such loops as (ARTICLES), (BREVES) or (RUBRIQUES), the date in question is the publication date of the article, of the news item or the latest modification date of the section.

If you insert a date tag (#DATE) outside any loops in a template, the date will be that of the current day — unless a date is passed in the page URL.

If a date is passed through the URL, then it is analysed through strtotime: thus ?date=2003, ?date=2003/01 will work, and so will date=-1year (a year ago), ?date=1march1970 (articles published on 1st March 1970), etc.

Date, Age and Relative Age criteria

The {age} criterion makes it possible to select articles according to the time span between their publication date and the current date. So, {age<30} will prevent articles older than 30 days from being displayed.

The {age_relatif} criterion (relative age) enables the comparison between the publication dates of two articles. If an article has just been selected in a loop, a second loop nested inside the first will be able to call the articles published in the week preceding this one, via : {age_relatif<=7}{age_relatif>=0} — and so on.

The {age} and {age_relatif} criteria make it possible to differentiate between two articles published on the same day. You can make loops that fetch "previous" or "next" articles:

<BOUCLE_art(ARTICLES){id_article}>
<BOUCLE_previous(ARTICLES){age_relatif>=0}{par date}{inverse}{1,1}>
previous : <a href='#URL_ARTICLE'>#TITRE</a> #DATE
</BOUCLE_previous>
<div><b>#TITRE</b> - #DATE </div>
<BOUCLE_next(ARTICLES){age_relatif<0}{par date}{0,1}> 
suivant : <a href='#URL_ARTICLE'>#TITRE</a> #DATE
</BOUCLE_next>
</BOUCLE_art>

Caution! In spite of appearances, date comparisons are quite tricky to manage. Because of "vague dates" (e.g. an article published in a given month, without any specific day being stated) "age_relatif" can result in a zero value in one direction, but not in the other! Hence the asymmetry of the loops shown above: in one direction you’re requesting the "second most recent" {age_relatif>=0} article (because the most recent, on a loose comparison, can only be the article itself); in the other direction you’re requesting the oldest published article from among the ones that were, strictly speaking, published later.

The {jour_relatif} (relative day), {mois_relatif} (relative month) and {annee_relatif} (relative year) criteria work like age_relatif, but take into account dates rounded off to the day, month, and year respectively. For instance, if the URL contains the variable ?date=2013-01-01, the following loop will fetch "all the articles published in the month of March 2013".

<h3>Articles of [(#DATE|nom_mois)] [(#DATE|annee)] :</h3>
<BOUCLE_blog(ARTICLES){mois_relatif=0}{par date}{"<br />"}>
<a href='#URL_ARTICLE'>#TITRE</a> ([(#DATE|jour)]/[(#DATE|nom_mois)]
</BOUCLE_blog>

Date of Earlier Publishing

If you’ve enabled the "Date of earlier publishing" option in the private area, most of the criteria presented above will work: just add _redac to the criterion. So, {age_redac>365} will display articles whose earlier publication date goes back to over a year. ("redac" means "writing", but in our case here: "publishing").

If a loop selects an article whose {date_redac} (date of earlier publication) is defined, an inner loop containing the {annee_relatif_redac=0} criterion will fetch articles whose earlier publication dates belong to the same year. (annee_relatif_redac translates as "relative year of publication").

An example of sorting articles by date on a site’s homepage

As an example, here’s how you can display all the articles of a site, sorted by publication months (usually coded in the sommaire.html) template:

<BOUCLE_articlem(ARTICLES){par date}{inverse}>
<BOUCLE_1st_of_themonth(ARTICLES){id_article}{doublons}>
<b> [(#DATE|nom_mois|majuscules)] [(#DATE|annee)] </b>
<ul>
   <li><a href="#URL_ARTICLE">[(#TITRE|couper{50})]</a> - [(#DATE|jour)]/[(#DATE|mois)]</li>
</BOUCLE_1st_of_themonth>
   <BOUCLE_month(ARTICLES) {mois_relatif=0}{doublons}{par date}{inverse} >
       <li><a href="#URL_ARTICLE">[(#TITRE|couper{50})]</a> - [(#DATE|jour)]/[(#DATE|mois)]</li>
   </BOUCLE_month>
</ul>
</BOUCLE_articlem>

Author bealach, Paolo Published : Updated : 01/07/23

Translations : عربي, català, Deutsch, English, Español, français, italiano, Nederlands