I'm trying to create a template on Wikipedia in French that changes the category of a page three weeks after the date it was added.
There will be a date
parameter, probably in the form DD/MM/YYYY
, and we can find out the current date using the magic words {{LOCALDAY2}}/{{LOCALMONTH2}}/{{LOCALYEAR}}
.
The problem is to find out whether the current date is 21 days older than the date on which the template was added.
It's easy if the two dates are in the same month, but more complicated if they are in different months, and even more so in different years.
Does anyone have a simple idea using wikicode or a regex?
Here is my current solution using {{LOCALWEEK}}
which gives the number of the current week (semaine
is the parameter containing the number of the week that the model was added), but I would prefer a ‘normal’ date:
{{#ifexpr: {{LOCALWEEK}} > {{#ifexpr: {{{semaine|0}}}>48 and {{LOCALWEEK}}<48 <!-- checks if added in December -->
| {{{semaine}}}-52 <!-- if in december, substracts 52 weeks -->
| {{{semaine}}} <!-- if not, uses the current week number -->
}} + 3 <!-- adds 3 weeks to the week the template was added -->
| [[:Catégorie:1]] <!-- if current week is less than 'semaine' + 3 -->
| [[:Catégorie:2]] <!-- if current week is more than 'semaine' + 3 -->
}}
This is trivial with {{#time}}
:
{{#ifexpr:
{{#time:U}} - {{#time:U|{{{added}}}}} > 3 * 7 * 24 * 60 * 60
|[[:Catégorie:1]]
|[[:Catégorie:2]]
}}
U
specifies that {{#time}}
should output an Unix timestamp. {{#time:U}}
is thus the "current" time, while {{#time:U|{{{added}}}}}
marks the moment the template was added. The expression on the right-hand side is just the number of seconds in three weeks.
One common pitfall is that the page might need to be purged. Otherwise, MediaWiki may just load the page from cache without rerendering, causing the categories to not be updated.