Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
Getting current date and time
Hi,
I’m using txp variables to store the current date and time,
and I receives different results inside individual article pages then in the main-page (section)
<txp:variable name="current_date"><txp:posted format="%d.%m.%y" /></txp:variable>
<txp:variable name="current_time"><txp:posted format="%H:%M" /></txp:variable>
I believe that into individual page it takes the article date & time instead of the current,
but that is even that the 2 variables are outside any <txp:article />
or <txp:article_custom />
.
So what’s the right way to get the current date & time please?
(with DST and timezone)
Offline
Re: Getting current date and time
I found this answer by ruud, so I changed my code to:
<txp:variable name="current_date"><txp:php>echo date('d.m.y', time() + tz_offset());</txp:php></txp:variable>
<txp:variable name="current_time"><txp:php>echo date('H:i', time() + tz_offset());</txp:php></txp:variable>
And it works good,
But it’s from 2007, so is there a cute magic txp-tag for it nowadays, or it is fine?
Offline
Re: Getting current date and time
Do you mean today’s date? If so then this:
<txp:php>echo date('c');</txp:php>
That will output the full date and time with timezone. List of all options are available here.
Last edited by philwareham (2012-07-28 12:11:35)
Offline
Re: Getting current date and time
If you need the time that follows Textpattern’s timezone settings (e.g. TZ offset, DST), you can use Textpattern’s safe_strftime
function. Calculating the timezone offset by yourself using tz_offset()
and supplying UNIX timestamp isn’t necessary if you use the function.
For instance the following returns the time as set in Textpattern’s preferences:
<txp:php>
echo safe_strftime('%Y.%m.%d %H:%M:%S');
</txp:php>
You can find all available formatting options from strftime’s documentation.
The following snippet using raw strftime()
and Phil’s snippet above (using date
and no timestamp) both return server’s raw time and ignore Textpattern’s preferences:
<txp:php>
echo strftime('%Y.%m.%d %H:%M:%S');
</txp:php>
If your site already uses rah_function plugin for other tasks, it can used to call those functions for you too, providing bit more XML like syntax:
<txp:rah_function
call="safe_strftime"
format="%Y.%m.%d %H:%M:%S"
/>
Offline
Pages: 1