Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-03-02 16:55:52

mrtunes
Member
From: Toronto, On
Registered: 2007-03-12
Posts: 575
Website

good article on date formatting

When you work with activity streams with current and older content, the way dates are presented to you matter a lot.
You see how apps get this wrong all over the place. They’ll display dates like “1 month ago”, giving an imprecise idea of when something exactly happened; or they’ll take the opposite approach and show you something like “2012-02-03 17:39:02 +00″, which is incredibly hard to read unless you’re a robot.

read the full article from teambox

Last edited by mrtunes (2012-03-02 16:56:45)

Offline

#2 2012-03-02 17:02:41

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

Re: good article on date formatting

mrtunes

Interesting. I can smell a plugin coming on to test the waters… maybe something like smd_calendar should do it? And if that part of it is any good, maybe even take the good bits and make, I dunno, a <txp:posted format="human" /> tag for core :-)

Last edited by Bloke (2012-03-02 17:03:04)


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

#3 2012-03-02 17:16:37

mrtunes
Member
From: Toronto, On
Registered: 2007-03-12
Posts: 575
Website

Re: good article on date formatting

that would be really cool! i was thinking about a set of conditionals to match up with what the article suggests, but a plugin would be much cleaner.

Offline

#4 2012-03-02 22:06:20

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

Re: good article on date formatting

Psssst beta of smd_date

No docs yet. But here’s a quick rundown:

  • <txp:smd_date> outputs the current date from the current context (if you’re in an article, it uses the posted date, if in a file_download it uses the file creation date, etc)
  • Use the when attribute to explicitly override / control when the date is taken from:
    • now ummm, right this second
    • posted: article posted date
    • expires: article expiry date
    • modified: article modified date
    • image_date: image upload date (a.k.a. image_created)
    • file_created: file creation (upload) date (a.k.a. file_date)
    • file_modified: file modification date
    • link_date: link creation date (a.k.a. link_created)
    • comment_date: comment posted date (a.k.a. comment_time / comment_posted)
    • any other (English-looking) date or timestamp = the given date
  • Use the format attribute as normal with the strftime codes. Also accepts format="human" which applies the logic in that article you mentioned in the OP (with the addition of ‘N seconds ago’, and ’1 minute ago’)

btw, you can supply more than one when (comma-separated) and use the usual wraptag / break / class attributes to split them up. Bear in mind that if you try to use two items at once in the same tag and you haven’t explicitly set the context then the plugin will issue warnings about “cannot use blah tags outside a blah context”. Still could be handy to display the posted, expires and modified in one tag though!

See how you go with the plugin. If it’s worthwhile I’ll make it into a proper release. Fedback / improvements welcome.


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

#5 2012-03-07 19:59:58

kvnmcwebn
Member
From: Ireland
Registered: 2007-01-27
Posts: 724
Website

Re: good article on date formatting

bloke would this support outputting a date in this format:
It would be really handy for space constraints and international date formatting.

"26 Feb 12" or "26 Feb 2012"

textpattern only supports:

"26 February 12" or "Feb 26"

Last edited by kvnmcwebn (2012-03-07 20:00:33)


its a bad hen that wont scratch itself.
photogallery

Offline

#6 2012-03-07 20:18:58

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

Re: good article on date formatting

kvnmcwebn wrote:

textpattern only supports: “26 February 12” or “Feb 26”

It only has those as presets, true. But you can format a date any way you wish if you use the format attribute to the posted, expires, modified, etc tags. e.g.

<txp:posted format="%d %b %y" />

to get 26 Feb 12. See all the formatting codes you can use. All the above plugin does is allow you to use the same tag for all dates or output multiple dates from one tag. And of course the format="human" attribute which alters the format attribute automatically based on how long ago the date occurred.

Last edited by Bloke (2012-03-07 20:21:37)


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

#7 2012-03-07 20:53:12

kvnmcwebn
Member
From: Ireland
Registered: 2007-01-27
Posts: 724
Website

Re: good article on date formatting

Oh thanks Bloke. Sorry for the oversight.


its a bad hen that wont scratch itself.
photogallery

Offline

#8 2012-03-07 21:45:31

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: good article on date formatting

Hello Stef, I can’t get the human format working.
I’ve tried with:

<txp:smd_date format="human" />

with no luck.
Can you help me, please?

Last edited by candyman (2012-03-07 21:45:56)

Offline

#9 2012-03-07 21:58:41

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

Re: good article on date formatting

candyman wrote:

I can’t get the human format working.

OK, is it just the format="human" that doesn’t work or does it not give you anything back for any regular format string?

Where are you using it, i.e. in what context? Inside an article tag/Form? A file_download_list>? An images list? The plugin needs a context in order to work. If you use the when attribute it uses that as the context. If not, it tries to pick one up automatically from the surrounding tags or, failing that, it’ll give you no output.

Last edited by Bloke (2012-03-07 21:59:38)


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

#10 2012-03-07 22:26:42

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: good article on date formatting

I use it in a form.
Using it insted of

<txp:posted />

it changes the date from Italian to the International/English format.

Last edited by candyman (2012-03-07 22:27:08)

Offline

#11 2012-03-07 22:41:47

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

Re: good article on date formatting

candyman wrote:

it changes the date from Italian to the International/English format.

Oh right, so it is working it’s just not doing what you expect! That’s different.

Yes, if you look at the article that mrtunes referenced, you’ll see that the output is very English. It uses ‘th’ and ‘st’ and other English ordinals in the date strings, because PHP doesn’t currently have any others for other languages. At least as far as I know.

The format characters I specified conform exactly to the ones in the above article.

Having said that, I’m using strings such as %b, which according to the PHP manual returns:

Abbreviated month name, based on the locale

So I would expect it to return the abbreviated month name in Italian if that’s what your locale is set to. Dates that have occured in the very recent past get exactly the same (not-very-international-friendly) treatment as those in the core; namely things like “a few seconds ago”.

Before I officially release the plugin I’d like to find a way to permit you to specify the formats for each breakpoint, but I haven’t figured that out yet. But even without that it should be returning the month names in your language. So what, exactly, are you seeing and what do you expect?


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

#12 2012-03-08 14:56:22

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: good article on date formatting

Surely I’m writing the code in the wrong way.
Can you tell me the tag I have to write to show the date of the post in the human way?
Something like: “Posted by candyman yesterday”.

Using it in the article_form , with the code

<txp:posted />

I get

25 LUGLIO, 22:02

whit the code

<txp:smd_date format="human" />

I get

LUG 25TH 2010

I expect

8 months ago (or , better, "8 mesi fa")

Hope it helps.

Offline

Board footer

Powered by FluxBB