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.
Hire 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.
Hire 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
phiw13 on Codeberg
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
phiw13 on Codeberg
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:postedtag 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.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Offline
Re: Outputting current date
Wow! Excellent! Was this something you’d tried before or did you concoct this just now? Either way, in very brief testing for now it works as advertised. Big thanks!!
Other time-related functionality I can think of is:
- compare against some specified time so that one can display something related to a particular time, e.g. “show from__” or “show until __”.
- formatting from->to dates. I currently have a custom solution for this, but what makes it tricky is finding a simple way to define attributes for the various combinations across time unit boundaries, e.g. 3 – 6 April 2024, 31 March – 2 April 2024, 25 December 2023 – 6 January 2024, etc. In German, I typically have two variants: one verbose as described and one concise with the 03. – 06.04.24, 31.03. – 02.04.24, 25.12.23 – 06.01.24, so I have these hard-coded in my own version.
Whether or not these pass the “bloatware” test ;-) I’m happy to have this new tag in our tool belt. Thank you.
TXP Builders – finely-crafted code, design and txp
Offline
Re: Outputting current date
Date ranges are an absolute mare. And virtually impossible to code across languages in a meaningful way.
I’ve got a delivery date range Shortcode for a client, and the Form that outputs that one line is about 175 lines of PHP.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: Outputting current date
Bloke wrote #336998:
Date ranges are an absolute mare. And virtually impossible to code across languages in a meaningful way.
I’ve got a delivery date range Shortcode for a client, and the Form that outputs that one line is about 175 lines of PHP.
Wow. Mine is obviously a lot simpler. This mini plugin is what I use for my own use.
TXP Builders – finely-crafted code, design and txp
Offline