Listview calculate age
OK, let's move on a bit. We have the manufacture_month per vehicle. So we can calculate the age.
This is just some PHP DateTime calculation, nothing fancy:
| // calculation of age (today - month of manufacture)
$manufacturemonth = $this->getProperty('manufacture_month'); // DateProperty object
$manufacturemonth_year_month = $manufacturemonth->getValue(); // "2026-01"
$temp = new DateTime($manufacturemonth_year_month); // DateTime object. Let's take the 1st or that month.
$today = new \Datetime(); // DateTime object / today by default
$diff = $today->diff($temp); // calculate the difference between manu-date and today
// for leasing contracts we usually calculate months like 48.
$diff_months = $diff->y * 12 + $diff->m; // let's forget about the days. We don't even know exact manuf-date, only month.
|
let me put this code into our getLabelHtml() function for now:
| public function getLabelHtml(): string
{
// calculation of age (today - month of manufacture)
$manufacturemonth = $this->getProperty('manufacture_month'); // DateProperty object
$manufacturemonth_year_month = $manufacturemonth->getValue(); // "2026-01"
$temp = new DateTime($manufacturemonth_year_month); // DateTime object. Let's take the 1st or that month.
$today = new \Datetime(); // DateTime object / today by default
$diff = $today->diff($temp); // calculate the difference between manu-date and today
// for leasing contracts we usually calculate months like 48.
$diff_months = $diff->y * 12 + $diff->m; // let's forget about the days. We don't even know exact manuf-date, only month.
// gather our data
$model = $this->getProperty('model');
$month = $this->getProperty('manufacture_month');
$details = [
$model?->toDisplayValue(),
$month?->toDisplayValue(),
$diff_months > 0 ? ($diff_months . " months") : null, // skip brand new ones
];
$details = array_filter($details);// remove empty entries
// create a tile
\System\UI\Output::use(); // important for autoloading
$tile = new \System\UI\Output\Elements\Tile('car', $this->licenseplate);
if ($details)
$tile->subtitle = implode(' • ', $details);
return $tile;
}
|
See how we just add the age to our $details-array in line 19. If this vehicle is at least one month old, we add the age. If not, that line will be filtered out by array_filter()-function in line 21.

Let's change the manufacture date to see what's happening:

After saving, we can see the changes:

- New value was saved
- Age calculated correctly
- Change was recorded in Audit Trail (history timeline)
Next
Replace age display by a calculated field