Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] How to use Member::currentUser()->DateFormat in the frontend

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

Page: 1
Go to End
Author Topic: 1477 Views
  • mmh
    Avatar
    Community Member
    24 Posts

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

    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.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

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

    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.

  • cuSSter
    Avatar
    Community Member
    56 Posts

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

    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

  • mmh
    Avatar
    Community Member
    24 Posts

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

    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.

  • cuSSter
    Avatar
    Community Member
    56 Posts

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

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

    1477 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.