Textpattern Forum

You are not logged in. Register | Login | Help

#1 2012-07-28 11:53:56

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 542
Website

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

#2 2012-07-28 12:05:03

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 542
Website

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

#3 2012-07-28 12:09:32

philwareham
Core designer
From: Farnham, Surrey, UK
Registered: 2009-06-11
Posts: 1,624
Website

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

#4 2012-07-28 13:56:06

Gocom
Developer
Registered: 2006-07-14
Posts: 4,476
Website

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"
/>

Rah-plugins | What? I’m a little confused… again :-) <txp:is_god />

Offline

Board footer

Powered by FluxBB