Skip to content

Core\Db\Row::date

$row->date(string $name): ?Core\Time\Date$row->date(
    string $name,
): ?Core\Time\Date

Reads one column of a row as a calendar day.

Core\Db\Row::date takes the name of a column and returns a Core\Time\Date. That is a day in the calendar, such as a birthday or the day an invoice is due. It carries no time of day and no time zone, because a DATE column carries neither.

The result is null when the column has no value in this row, so the type you get back is ?Core\Time\Date. Test it for null before you use it.

The method throws a LogicError when the row has no column with that name, and when the column is not a DATE. A column that also holds a time of day is read with Core\Db\Row::instant, and a clock reading with Core\Db\Row::time.

The examples below print the day a task is due, read a column that may have no value, and pick out the tasks whose day has passed.

Parameters

  • $namestringThe column label, as the server described it.

Return value

Returns ?Core\Time\Date. The day, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, or the column is not a DATE.

Examples

The examples below run with this configuration file.

nvs.toml
# The `notes` database is kept in memory. It is deleted when the program ends.
[db.notes]
driver = "sqlite"
path = ":memory:"
# The program may open the `notes` database.
[capabilities.db]
connect = ["notes"]

Read the day a task is due

<?nvs
// Shows how to read a calendar day out of a row.
var $db = Core\Db::connect('notes');
$db->execute('create table tasks (title text not null, due date not null)', []);
$db->execute('insert into tasks (title, due) values (?, ?), (?, ?)', ['pay the rent', '2026-02-01', 'call Ana', '2026-01-19']);
// `date` reads the named column as a `Core\Time\Date`, and `format` writes that
// day out. This prints `call Ana: 2026-01-19` and `pay the rent: 2026-02-01`.
foreach ($db->query('select title, due from tasks order by due', []) as Core\Db\Row $row) {
?Core\Time\Date $due = $row->date('due');
if ($due != null) {
echo $row->string('title') ?? '', ': ', $due->format('yyyy-MM-dd'), "\n";
}
}
$db->close();
Output
call Ana: 2026-01-19
pay the rent: 2026-02-01

A column that has no value

<?nvs
// Shows what you get when the day column has no value in this row.
var $db = Core\Db::connect('notes');
$db->execute('create table tasks (title text not null, due date)', []);
$db->execute('insert into tasks (title, due) values (?, ?), (?, ?)', ['buy milk', null, 'pay the rent', '2026-02-01']);
// `date` returns `null` for a column with no value, so your program says what
// to print instead. This prints `buy milk: no day` and the day of the other row.
foreach ($db->query('select title, due from tasks order by title', []) as Core\Db\Row $row) {
?Core\Time\Date $due = $row->date('due');
echo $row->string('title') ?? '', ': ';
if ($due != null) {
echo $due->format('yyyy-MM-dd'), "\n";
} else {
echo "no day\n";
}
}
$db->close();
Output
buy milk: no day
pay the rent: 2026-02-01

Pick out the tasks that are late

<?nvs
// Shows how to find the tasks whose day has passed. The day compared with is
// written out here, so this program prints the same lines every time.
var $db = Core\Db::connect('notes');
$db->execute('create table tasks (title text not null, due date not null)', []);
$db->execute(
'insert into tasks (title, due) values (?, ?), (?, ?), (?, ?)',
['call Ana', '2026-01-19', 'pay the rent', '2026-02-01', 'book the train', '2026-03-08'],
);
var $today = Core\Time\Date::at(2026, 2, 1);
// `compareTo` returns a number below zero when the day is before the other
// day. This prints `call Ana is late`, and nothing for the two later tasks.
foreach ($db->query('select title, due from tasks order by due', []) as Core\Db\Row $row) {
?Core\Time\Date $due = $row->date('due');
if ($due != null) {
if ($due->compareTo($today) < 0) {
echo $row->string('title') ?? '', " is late\n";
}
}
}
$db->close();
Output
call Ana is late