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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

$defaults, DateFields with Silverstripe 2.4


Go to End


5 Posts   4086 Views

Avatar
Myrdhin

Community Member, 70 Posts

28 May 2010 at 10:32pm

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;
	}
}

Avatar
Myrdhin

Community Member, 70 Posts

31 May 2010 at 7:56pm

Edited: 31/05/2010 7:56pm

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

Avatar
Myrdhin

Community Member, 70 Posts

31 May 2010 at 9:07pm

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 !

Avatar
Myrdhin

Community Member, 70 Posts

31 May 2010 at 9:11pm

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

Avatar
Myrdhin

Community Member, 70 Posts

1 June 2010 at 10:23pm

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