This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Trying to figure out if there's a way to display a date for a particular timezone for list views in the admin. I want the values to be stored as UTC but all the administrators will be looking at it in a single timezone. So, even though the value stored is in UTC, it should display as America/Chicago
. I get that in views where you have full control over the date you can pass a timezone flag with the date filter but for things like the column types, timezone is not an option.
Add the following in your back-end controller:
public function listOverrideColumnValue($record, $columnName, $definition) {
if ($columnName == 'created_at') {
// apply local timezone
$createdAt = (new \DateTime($record->created_at))->setTimezone(new \DateTimeZone('America/Chicago'));
return $createdAt->format('d.m.Y g:ia');
}
}
Last updated
You can convert timestamps without using DateTime
just by using the value from the database. Just make sure you $record[$columnName]
is not null
otherwise it will cause an error.
public function listOverrideColumnValue($record, $columnName, $definition) {
if (in_array($columnName, ['created_at', 'updated_at', 'scheduled_at'])) {
return $record[$columnName]->setTimezone('...');
}
}
You can also use use \Carbon\Carbon
and parse $record[$columnName]
if you like to manipulate the timestamp. For example:
return Carbon::parse($record[$columnName])->addMinutes(5)->setTimezone('...')
By using Carbon you don't have to check for null
values. More on https://carbon.nesbot.com/docs/
1-5 of 5