Jump to:

21301 Posts in 5736 Topics by 2603 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2481 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;
       }
    }

  • Anonymous user
    Avatar
    Community Member
    1 Post

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

    From the PHP manual:

    The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

    You may want to check:

    1. Is the default obeyed for values after 1970?
    2. Is your system running an old version of PHP?

    Cheers,
    - Luke

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

  • Anonymous user
    Avatar
    Community Member
    1 Post

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

    Ah, I didn't notice the strtotime() call before. That's the issue.

    You may indeed have to extend Date, but this issue should be dealt with in the core, really. Thanks for raising this issue. Perhaps you'd like to make a bug report?

    Thanks,
    - Luke

  • 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

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