Jump to:

23372 Posts in 18175 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » $defaults, DateFields with Silverstripe 2.4

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: 3207 Views
  • Myrdhin
    Avatar
    Community Member
    67 Posts

    $defaults, DateFields with Silverstripe 2.4 Link to this post

    Hello,

    I would like to set DateField default values (01/01/1900) but i can't : it's always the same date : 01/01/1970 !?

    I try :

    • 01/01/1900
    • 1900/01/01
    • 01-01-1900
    • 1900-01-01

    but nothing works :'(

    Could you help me ? Thanks.

    My code :

    class Document extends Page {

       static $db = array(
          'Begin' => 'Date',
          'End' => 'Date',
       );

       static $defaults = array(
          'Begin' => '01/01/1900',
          'End' => '01/01/1900',
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();

          $BeginField = new DateField('Begin', 'Begin'));
          $EndField = new DateField('End', 'End'));

          $BeginField->setConfig('dmyfields', true);
          $EndField->setConfig('dmyfields', true);

          $fields->addFieldToTab('Root.Content.Main',
             new FieldGroup(
                new LiteralField('Dates', 'Dates'),
                new FieldGroup($BeginField, $EndField)
             ),
             'Content'
          );
          
          return $fields;
       }
    }

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: $defaults, DateFields with Silverstripe 2.4 Link to this post

    Thanks for your response.

    I try :

    static $defaults = array(
       'Begin' => '1990-01-01',
       'End' => '1990-01-01'
    );

    and it works fine Thanks.

    But how can i have a date before 1970 ? I would like to have date like 1800, or 1900...

    ps : my system :

    • Debian Lenny (5.0.4)
    • Apache 2 (2.2.9-10)
    • PHP5 (5.2.6)
    • MySQL Server 5 (5.0.51a)
    • Silverstripe 2.4.0

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: $defaults, DateFields with Silverstripe 2.4 Link to this post

    I think i've found where the "problem" come from. In file sapphire/core/model/fieldtypes/Date.php, in setValue() method :

    function setValue($value) {
       // @todo This needs tidy up (what if you only specify a month and a year, for example?)
       if(is_array($value)) {
          if(!empty($value['Day']) && !empty($value['Month']) && !empty($value['Year'])) {
             $this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
             return;
          }
       }

       // Default to NZ date format - strtotime expects a US date
       if(ereg('^([0-9]+)/([0-9]+)/([0-9]+)$', $value, $parts)) {
          $value = "$parts[2]/$parts[1]/$parts[3]";
       }

       if(is_numeric($value)) {
          $this->value = date('Y-m-d', $value);
       } elseif(is_string($value)) {
          $this->value = date('Y-m-d', strtotime($value));
       }
    }

    The use of

    date('Y-m-d', ... );

    is a problem... I think i'll try to create a MyDate class 'extends Date ?) with the use of the new DateTime class of PHP 5.2+ to resolve my problem.

    Or perhaps there is another solution ?

    Thanks for your help !

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: $defaults, DateFields with Silverstripe 2.4 Link to this post

    If I set $defaults like this :

    static $defaults = array(
       'Begin'   =>   array('Year' => '1900', 'Month' => '01', 'Day' => '01'),
       'End'   =>   array('Year' => '1900', 'Month' => '01', 'Day' => '01')
    );

    it works because the date() fonction isn't use in setValue() :

    function setValue($value) {
       
       ...
       
       if(is_array($value)) {
          if(!empty($value['Day']) && !empty($value['Month']) && !empty($value['Year'])) {
             $this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
             return;
          }
       }
       
       ...
    }

    but this doesn't resolve the problem when we change a date in the admin interface. I'll try my MyDate idea...

  • Myrdhin
    Avatar
    Community Member
    67 Posts

    Re: $defaults, DateFields with Silverstripe 2.4 Link to this post

    Thanks you too

    I'm trying to extend Date and use PHP5.2+ DateTime class in it. It seems to work.
    When it will be finished, i'll post my extended class here. Perhpas this could help somebody ;p

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