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.

General Questions

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

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

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


5 Posts   1499 Views


2 November 2010 at 1:12am Community Member, 24 Posts

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.


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.

Ryan M.

2 November 2010 at 10:45am Community Member, 309 Posts

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.


2 November 2010 at 11:52pm Community Member, 56 Posts

There are predefined date formats that you can use. Nice and Long, to name a few. You can read on:

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.



3 November 2010 at 9:15am (Last edited: 3 November 2010 10:01am), Community Member, 24 Posts

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).


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

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('');

Add the extension in your _config.php via

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

Now you can access your datefield in the template via


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


3 November 2010 at 6:46pm Community Member, 56 Posts

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