Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Outputting current date
[ split off from this post and this post (my fault, I did the split badly). I have quoted the missing preceding post in this article. ]
phiw13 wrote #336011:
[ split off from this post ]
I was surprised that the PHP prefs, and particularly in articles, was turned ON by default. Luckily lower-level users/authors can’t include anything PHP in articles.
Flipping the pref(disabled by default) is perfectly fine, even expected :-).
My use of PHP in pages/forms is very limited nowadays – one stood out when checking on a live site: a snippet for the email body of contact forms (com_connect plugin, fetching the value of a select widgets for output in the email, as documented by the plugin). Or another one, the current year, for a website-wide copyright. Something you cannot fetch with TXP tags.
<p>©2004-<txp:php> echo gmdate('Y'); </txp:php> - me </p>...
phiw13 wrote #336011:
the current year, for a website-wide copyright. Something you cannot fetch with TXP tags.
I believe Oleg tweaked the <txp:posted />
tag (might be 4.9 but I think it made it into 4.8 – memory is hazy here) so you could specify an arbitrary date. Whether it allows you to do “this year” I’m not sure. If not, it would be a handy addition as I always resort to PHP for this.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Offline
Re: Outputting current date
Perfect. I seemed to remember you posting examples like this before but couldn’t find them.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Re: Outputting current date
Easy:
<txp:posted gmt time="now" format="%Y" />.
Ooh. Another PHP snipped out not really. Thank you.
–^–^–
Edit I take that (partly) back. I really should test in the context I actually use it…
The quoted txp:posted
tag works fine on an individual article page, but not on list pages.
Tag error: <txp:posted gmt time="now" format="%Y" /> -> Textpattern Notice: Article tags cannot be used outside an article context.
a <txp:date />
tag?
–^–^–
I think this was the (a?) previous discussion Bloke mentions above.
Last edited by phiw13 (2023-12-06 00:17:04)
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Outputting current date
etc wrote #336016:
Easy:
<txp:posted gmt time="now" format="%Y" />
.
just to confirm. this doesn’t yet work on 4.8.8 as I just tried it and no DATE
was output.
…. texted postive
Offline
Re: Outputting current date
Would be interesting to understand how often and under what circumstances PHP code is in use in the content of a) articles and b) pages.
I am rather ashamed to keep PHP enabled in forms for just one small reason — I do not know how to display an URL of the requested web page in a simpler way than
$urn = urldecode($_SERVER['REQUEST_URI']);
$urh = "{$_SERVER['HTTP_SCHEME']}://{$_SERVER['HTTP_HOST']}";
$url = "{$_SERVER['HTTP_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
echo '<a rel="nofollow noreferrer noopener" style="text-decoration:none" href="' . $escaped_url . '">' . $urh . $urn . '</a>';
I have applied it for 404 response only.
Offline
Re: Outputting current date
I think that <txp:page_url type="request_uri" />
will help you there. In your example, it would output /nowhere
.
Documentation is here
Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern
Offline
Re: Outputting current date
Oh, I missed the types – it’s a treasure, thank you very much!
Offline
Re: Outputting current date
Bloke wrote #336015:
I believe Oleg tweaked the
<txp:posted />
tag (might be 4.9 but I think it made it into 4.8 – memory is hazy here) so you could specify an arbitrary date. Whether it allows you to do “this year” I’m not sure. If not, it would be a handy addition as I always resort to PHP for this.
phiw13 wrote #336030:
Ooh.
Another PHP snipped outnot really. Thank you.–^–^–
Edit I take that (partly) back. I really should test in the context I actually use it…
The quoted
txp:posted
tag works fine on an individual article page, but not on list pages.
Tag error: <txp:posted gmt time="now" format="%Y" /> -> Textpattern Notice: Article tags cannot be used outside an article context....
a
<txp:date />
tag?–^–^–
I think this was the (a?) previous discussion Bloke mentions above.
Just rooted around to find this post, and tried it out – but as Philippe says, I had no luck getting output in a non-article situation. It’s not a big deal to use <txp:evaluate query="date('Y')" />
as long as one remembers to enable date
for txp:evaluate in the advanced settings, but it would be neat to be able to easily output/format arbitrary dates, especially with international output.
Another thing I often farm out to txp:php
blocks or shortcodes is formatting from-to date(/time) ranges for event lists. That would be handy in a function. Perhaps this is more plugin territory?
TXP Builders – finely-crafted code, design and txp
Offline
Re: Outputting current date
I’ve just added <txp:date />
tag to dev, please test and suggest feature ideas. Currently it works e.g. like this:
<txp:date gmt time="now" lang="de" calendar="hebrew" format="%Y %b" />
You can also set type
attribute to retrieve any txp date field:
<txp:date type="file" time="created" />
but this will only work in file context. Old date-related tags work like before.
Offline
Re: Outputting current date
That. Is. Amazing.
Another chunk of PHP eradicated from my templates. Thank you.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
Offline