Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-12-05 10:36:55

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,445
Website GitHub

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

Online

#2 2023-12-05 10:43:12

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: Outputting current date

Easy: <txp:posted gmt time="now" format="%Y" />.

Offline

#3 2023-12-05 12:36:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,445
Website GitHub

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

Online

#4 2023-12-05 22:35:27

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,187
Website

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

#5 2023-12-06 00:14:00

bici
Member
From: vancouver
Registered: 2004-02-24
Posts: 2,091
Website Mastodon

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

#6 2023-12-07 17:23:33

Vienuolis
Member
From: Vilnius, Lithuania
Registered: 2009-06-14
Posts: 310
Website GitHub GitLab Mastodon Twitter

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

#7 2023-12-08 00:25:19

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,187
Website

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

#8 2023-12-08 07:17:21

Vienuolis
Member
From: Vilnius, Lithuania
Registered: 2009-06-14
Posts: 310
Website GitHub GitLab Mastodon Twitter

Re: Outputting current date

Oh, I missed the types – it’s a treasure, thank you very much!

Offline

#9 2024-03-31 13:56:04

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

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 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.

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

#10 2024-03-31 16:29:39

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

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

#11 2024-03-31 17:56:02

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,445
Website GitHub

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

Online

#12 2024-03-31 18:19:28

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: Outputting current date

It’s yet time to bloat it with extra features ;-)

Offline

Board footer

Powered by FluxBB