0
votes

In Silverstripe 4, in a GridField, how do we put the date in the d-m-Y format with these fields?

private static $summary_fields = [
    'Created',
    'LastEdited',
];

private static $field_labels = [
    'Created' => 'Criado',
    'LastEdited' => 'Logado',
];
1

1 Answers

1
votes

To display date in a specific format we can create a function to return the formatted date.

The following code will display the Created and LastEdited fields in the d-m-y format:

private static $summary_fields = [
    'CreatedDMY',
    'LastEditedDMY',
];

private static $field_labels = [
    'CreatedDMY' => 'Craido',
    'LastEditedDMY' => 'Logado',
];

public function getCreatedDMY() {
    return $this->dbObject('Created')->format('dd-MM-y');
}

public function getLastEditedDMY() {
    return $this->dbObject('LastEdited')->format('dd-MM-y');
}

If we want to display the time as well we would change the format to dd-MM-y HH:mm:ss

public function getCreatedDMY() {
    return $this->dbObject('Created')->format('dd-MM-y HH:mm:ss');
}

public function getLastEditedDMY() {
    return $this->dbObject('LastEdited')->format('dd-MM-y HH:mm:ss');
}