Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Formatting Dates


Go to End


5 Posts   3354 Views

Avatar
adiwidjaja

Community Member, 14 Posts

23 April 2008 at 12:55am

Edited: 23/04/2008 12:56am

Hi!

We just solved a problem for us that I think all non-English users of silverstripe have: The Date Fieldtype uses the php function date(), which does not respect the setlocale settings:

//sapphire/core/model/fieldtypes/Date.php
if($this->value) return date($formattingString, strtotime($this->value));

Output: "February 2008" on a German computer.

Instead we use strftime:

if ($this->value) return strftime($formattingString, strtotime($this->value));

Output: "Februar 2008".

Is there any matter why date() is used? Otherwise I would propose to change this or to add a function strftime.

Cheers,

Andy

Avatar
Sam

Administrator, 690 Posts

23 April 2008 at 1:55pm

This seems like a reasonable change. Can you submit a patch to http://open.silverstripe.com?

Avatar
adiwidjaja

Community Member, 14 Posts

23 April 2008 at 7:40pm

Yes, of course! As this is my first patch for silverstripe: This is a Patch for "Core" against the trunk (aka 2.2.2RC3) for Milestone 2.2.3, component "Sapphire Framework", right? I will check, whether there are other places where date() is used.

Ah, what do you prefer? A general change towards strftime would require to change any format strings, as the syntax is different ("d.m.Y" vs. "%d.%m.%Y"). I still would prefer this as the standard way to format dates in silverstripe should be localized (hmm, does the word "localizable" exist?). I would track all uses of format strings in the core and in modules (I think mainly the blog module) and change them.

The alternative would be an additional function strftime().

Cheers,

Andy

Avatar
DiabolicDevilX

Community Member, 1 Post

7 June 2008 at 11:28am

A better solution would probaply be just adding a function called "LocalFormat" or something, which would wrap strftime as described.
This wouldn't break compatibility and features like pre-formatted ISO dates (which strftime doesn't currently supply) wouldn't be gone.

Avatar
Laax

Community Member, 14 Posts

27 September 2008 at 2:30am

Hi,
Thank you very much your code helped a lot but,
I think you should consider to add a utf8_encode() this way:

function LocalFormat($formattingString) {
		if($this->value) return utf8_encode(strftime($formattingString, strtotime($this->value)));
	}

because it helps for french language