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

Problems with the field Date


Reply


7 Posts   272 Views

Avatar
maga

Community Member, 11 Posts

9 May 2014 at 2:40am

Using Silverstripe version 2.4 ==== ===

I created a date field in my file artista.php

<? php
class Artist extends DataObject
  {
public static $ db = array (
"BirthDate" => "Date"
);

From the CMS insert the date in the following format (mm / dd / yyyy), but just saved, the system transforms me the date (dd / mmm / yyyy) while in my template/html I display the date in the following format (yyyy / mm / gg)
I would like to unify all formats (dd / mm / yyyy)

In summary:
input into the CMS = 19/04/2014
after saving visulaizzo = 19/apr/2014
the template / html = 2014/04/19

Thank you all in advance for the help that you will give me
Have a nice day
MG

Avatar
swaiba

Forum Moderator, 1799 Posts

13 May 2014 at 12:59am

I use the following for a UK date format... hope it helps...

class MyObj extends DataObject() {
...
static $db = array('MyObjDate' => Date)
...
function getCMSFields() {
$fields = parent::getCMSFields();
...
$df = new DateField('MyObjDate',$strLabel);
$df->setConfig('showcalendar', true);
$df->setLocale('en_GB');
$df->setConfig('dateformat', 'dd/MM/yyyy');
$fields->replaceField('MyObjDate',$df);
...
return $fields;
}

Avatar
maga

Community Member, 11 Posts

14 May 2014 at 1:16am

Hi Swaiba,
thanks for your help but I was not able to apply your help.
I have tried various combinations of your code but when I enter in the dataobject to the back end, out the error :
Undefined variable Fields

I look forward to any other suggestions
Thank you all in advance.
greetings
MG

Avatar
swaiba

Forum Moderator, 1799 Posts

14 May 2014 at 2:03am

Hi,

You are correct that exact code isn't tested - but it was cut/pasted from well used production code.

Which version are you using?
Do you have any experience in using getCMSFields to manipulate the fields?

Avatar
maga

Community Member, 11 Posts

15 May 2014 at 1:08am

Hi Swaiba,
thanks for your answer, unfortunately, are only 2 months I program with SilverStripe, I still have a little experience.
Below you carry the full code of dataobject, it is very easy.

=== I use Silverstripe version 2.4 ====

Artista.php

<?php
class Artista extends DataObject {
   public static $db = array(
      "Nome" => "Text",
      "Cognome" => "Text",
      "DataNascita" => "Date", // This is the date Field       
   );
   
   
   static $searchable_fields = array(
   "Nome" => "Nome Artista",
   "Cognome" => "Cognome Artista"   
   );   

static $summary_fields = array(
   "Nome" => "Nome Artista",
   "Cognome" => "Cognome Artista",
   );
   

      public function getCMSFields()
   {
      return new FieldSet(
         
         new TextField('Nome'),
         new TextField('Cognome'),         
         new DateField('DataNascita', 'Data Nascita') //this is the date field          
   
      );
   }

Thank you all in advance for the help that you will give me
Have a nice day
MG

Avatar
Tim Snadden

Community Member, 32 Posts

15 May 2014 at 7:46am

Maybe look at setting i18n::set_date_format() or i18n::set_default_locale() in _config.php?

Avatar
maga

Community Member, 11 Posts

15 May 2014 at 8:04pm

Hi Swaiba,
thanks for your answer, in my_config.php there's :

....
i18n::set_locale('it_IT'); //ITALY
.....

Thank you all in advance for the help that you will give me
Have a nice day
MG