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: 12,463
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.

Hire 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: 12,463
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.

Hire 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: 12,463
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.

Hire 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: 12,463
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.

Hire 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: 12,463
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.

Hire 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

#13 2012-03-08 15:11:30

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,463
Website GitHub

Re: good article on date formatting

candyman wrote:

I expect 8 months ago (or , better, “8 mesi fa”)

Re-check the start of the article, upon which the format="human" is based:

If the event happened this hour: 18 minutes ago
If it happened today: 3:49 pm
If it happened in the last two days: Wed 1:26 pm
If it happened this week: Thursday 8 am
If it was this month: Feb 27th
If it was this year: Jan 19th
If it was before: Jun 6th 2010

Thus if it occurred 8 months ago, it’s ‘last year’ which means it’s using the last format in the list. If you want it to use ‘n’ months ago you’re out of luck, sorry. Txp will only offer N days ago (I think) if you change the archive date format in prefs, but I think that’s it.

I could see if there’s a way to expose the number of days, months and years ago as a special code in the format string. Currently there’s only one special code SMD_ORDINAL which allows you to display the English ‘rd’, ‘th’ ‘st’ etc in your format attribute.


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

#14 2012-03-08 15:20:36

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

Re: good article on date formatting

I’ve understood, thanks.

I hope that in the next release you’ll be able to specify the formats for each breakpoint and, maybe, to find a solution for the ordinals localization (more difficult to achieve, I think).

Anyway

If it was this month: Feb 27th
If it was this year: Jan 19th

aren’t too similar?

For “If it was this year” I’d specify the number of months (one month ago, two months ago… eleven months ago) and, at the end, “last year”, “two years ago” and more. But, probably, in English it sounds better in your way. Just my thought, thanks again.

Last edited by candyman (2012-03-08 15:36:43)

Offline

#15 2012-03-08 17:17:10

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,463
Website GitHub

Re: good article on date formatting

candyman wrote:

I hope that in the next release you’ll be able to specify the formats for each breakpoint

Haha! So do I.

find a solution for the ordinals localization

Unfortunately, if PHP doesn’t support it, I can’t. Would love to find out how to do it though. I mean, in French I think ‘er’ is the only one used (correct me if I’m wrong but I’m sure I’ve seen 3{er} in use). I don’t know about other languages but I suppose I could add an attribute to let you specify them like this:

ordinals="st,nd,rd,th"

So in French you’d do:

ordinals="er"

(?) But in Italian for example it gets more complicated because it differs based on gender doesn’t it? But I guess if the ‘thing’‘ you are referring to is always a date then it’s only going to be of one gender… umm right? Or are the different months of different genders? If anybody has any ideas on this then please let me know.

If it was this month: Feb 27th / If it was this year: Jan 19th

Yes I spotted that they were the same so I just opted for one breakpoint.

For “If it was this year” I’d specify the number of months (one month ago, two months ago… eleven months ago) and, at the end, “last year”, “two years ago” and more.

That could be a tall order given the language strings involved but I’ll see if I can rustle something up. Won’t be immediately though.. got another plugin I’m hammering away at now and it’s BIG.


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

Board footer

Powered by FluxBB