Date Column

DateColumn uses the DataTables date type for chronological sorting. The column expects values formatted as ISO 8601 strings by default (Y-m-d). Pass a custom format when your data uses a different pattern.

Basic Usage

use Pentiminax\UX\DataTables\Column\DateColumn;

DateColumn::new('createdAt', 'Created');

Returns values as-is. mapRow() must return a string in Y-m-d format (or the configured format) for correct sorting.

Custom Format

Use setFormat() to declare the date format of your values:

DateColumn::new('publishedAt', 'Published')
    ->setFormat('d/m/Y');

Pass null to revert to the default Y-m-d:

$column->setFormat(null);

Display Formatting

To display dates in a different format than the raw stored value, use setRender() for client-side formatting:

DateColumn::new('createdAt', 'Created')
    ->setRender('function(data) {
        if (!data) return "";
        var d = new Date(data);
        return d.toLocaleDateString("fr-FR");
    }');

API Reference

Method Description
`DateColumn::new(string $name, string $title = '')`Creates a new DateColumn (type: `date`). Default format: `Y-m-d`.
`setFormat(?string $format)`Set the expected date format; `null` resets to default
`getFormat()`Returns the active format string

Complete Example

use Pentiminax\UX\DataTables\Attribute\AsDataTable;
use Pentiminax\UX\DataTables\Column\DateColumn;
use Pentiminax\UX\DataTables\Column\NumberColumn;
use Pentiminax\UX\DataTables\Column\TextColumn;
use Pentiminax\UX\DataTables\Model\AbstractDataTable;

#[AsDataTable(Article::class)]
final class ArticlesDataTable extends AbstractDataTable
{
    public function configureColumns(): iterable
    {
        yield NumberColumn::new('id', 'ID');
        yield TextColumn::new('title', 'Title');
        yield DateColumn::new('publishedAt', 'Published');
        yield DateColumn::new('updatedAt', 'Updated')->setFormat('Y-m-d H:i');
    }

    protected function mapRow(mixed $item): array
    {
        return [
            'id'          => $item->getId(),
            'title'       => $item->getTitle(),
            'publishedAt' => $item->getPublishedAt()?->format('Y-m-d'),
            'updatedAt'   => $item->getUpdatedAt()?->format('Y-m-d H:i'),
        ];
    }
}