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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

[SOLVED] How to use Member::currentUser()->DateFormat in the frontend


Go to End


5 Posts   3705 Views

Avatar
mmh

Community Member, 24 Posts

2 November 2010 at 1:12am

Hi there,

I'm currently playing around with a DataObject. The DataObject has a date field in the database:

static $db = array(
	'Date' => 'Date'
);

Now, in the frontend I can access the Date via "$Date" and can set a custom format in the template using e.g.

$Date.Format(d.m.Y)

But how is it possible to use the custom user settings? - Every user can change his preferred time and date format in the backend. But how is it possible for me to use this in the frontend?
Something like

public function Date() {
	if(Member::currentUser() && Member::currentUser()->ID) {
		return date(Member::currentUser()->DateFormat, strtotime($this->Date));
	}
	return $this->Date;
}

is not possible because the DateFormat in the Member Object was not stored in the php way to use date functions.

Avatar
Ryan M.

Community Member, 309 Posts

2 November 2010 at 10:45am

How is the date format being stored? You will probably most likely have to write a function that interprets the stored format and applies it in a PHP compliant way.

Avatar
cuSSter

Community Member, 56 Posts

2 November 2010 at 11:52pm

There are predefined date formats that you can use. Nice and Long, to name a few. You can read on:
http://api.silverstripe.org/2.4/sapphire/model/Date.html

You can add an additional db property for that object for his/her preferred date format:

static $db = array( 'DateFormat' => 'Varchar(20)' );

or use 'DateFormat' => "Enum('value1, value2, value3')" to limit the possible formats to those only supported by the Date class.

Then in your function have a switch statement to return the date in the desired format.

/C

Avatar
mmh

Community Member, 24 Posts

3 November 2010 at 9:15am

Edited: 03/11/2010 10:01am

Hey Ryan,

the date is stored in the Database in the default way (2010-11-02). And you're right, I've too write an extension or something like that. I can't find any Silverstripe way to handle this.

Hey Pinoy,
this is not what I'm searching for. Take a look at the member object and there you can see that every user can set his preferred date and time settings. I just wan't that a logged in user sees his settings on a page (like a news entry). Otherwise these settings are wasteful (for me).

Solution:

I've written an little Extension, hope this will be helpful for someone (rarely tested):

<?php
class DateExtension extends Extension {
	
	public function UserFormat() {
		require_once 'Zend/Date.php';
		if(!$this->owner->value || !Zend_Date::isDate($this->owner->value, 'YYYY-mm-dd')) {
			user_error('This is not a valid date value.', E_USER_ERROR);
		}
		$date = new Zend_Date($this->owner->value);
		if(Member::currentMember() && Member::currentMember()->DateFormat) {
			return $date->toString(Member::currentMember()->DateFormat);
		}
		return $date->toString('dd.mm.YYYY');
	}
	
}

Add the extension in your _config.php via

Object::add_extension('Date', 'DateExtension');

Now you can access your datefield in the template via

$Date.UserFormat

and the date will be displayed in the way the user has stored it in the database.

Avatar
cuSSter

Community Member, 56 Posts

3 November 2010 at 6:46pm

Oh, that's nice. Wasn't aware of that function in Member. Thanks for the info. :)