Skip to content

Core\Db\Row::instant

$row->instant(string $name): ?Core\Time\Instant$row->instant(
    string $name,
): ?Core\Time\Instant

Reads one column of a row as a moment in time.

Core\Db\Row::instant takes the name of a column and returns a Core\Time\Instant. That is one moment on the world clock, such as the second an order was paid. The column has to carry its own time zone. TIMESTAMPTZ in PostgreSQL and datetimeoffset in SQL Server are the two column types that do.

The result is null when the column has no value in this row, so the type you get back is ?Core\Time\Instant. 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 carries no time zone of its own. SQLite stores no time zone with a value, so no column of a SQLite database can be read this way. A column that holds a calendar day is read with Core\Db\Row::date, and a clock reading with Core\Db\Row::time.

Parameters

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

Return value

Returns ?Core\Time\Instant. The instant, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, or the column is not a timestamp with a time zone. A timestamp without a time zone is a Core\Time\DateTime in the time zone set for the connection.

Examples

The examples below run with this configuration file.

nvs.toml
# These examples use the PostgreSQL server on this computer at port 15432.
# `tls_ca_file` is the certificate that the server's certificate is checked against.
[db.shop]
driver = "postgres"
host = "127.0.0.1"
port = 15432
user = "novis"
password = "Novis-Test-Pw1"
database = "novis_test"
tls_ca_file = "../../../../../tests/db/ca.crt"
# The program may open the `shop` database.
[capabilities.db]
connect = ["shop"]

Read when an order was paid

<?nvs
// Shows how to read the moment an order was paid out of a row.
// requires: postgres
var $db = Core\Db::connect('shop');
// A temporary table is deleted when the connection closes.
$db->execute('create temporary table orders (id integer not null, paid_at timestamptz not null)', []);
$db->execute('insert into orders (id, paid_at) values (?, ?)', [1, '2026-03-01 15:00:00+01']);
// `instant` reads the column as a `Core\Time\Instant`. `toIso` writes it in UTC.
// The order was paid at 15:00 in a zone one hour ahead of UTC, so this prints `2026-03-01T14:00:00Z`.
foreach ($db->query('select paid_at from orders where id = ?', [1]) as Core\Db\Row $row) {
?Core\Time\Instant $paid = $row->instant('paid_at');
if ($paid != null) {
echo 'paid at ', $paid->toIso(), "\n";
}
}
$db->close();
Output
paid at 2026-03-01T14:00:00Z

An order that is not paid

<?nvs
// Shows what `instant` returns for a column with no value.
// requires: postgres
var $db = Core\Db::connect('shop');
$db->execute('create temporary table orders (id integer not null, paid_at timestamptz)', []);
$db->execute('insert into orders (id, paid_at) values (?, ?), (?, ?)', [1, '2026-03-01 14:00:00+00', 2, null]);
// Order 2 has no value in `paid_at`, so `instant` returns `null` for it.
// This prints `order 1: paid` and `order 2: not paid yet`.
foreach ($db->query('select id, paid_at from orders order by id', []) as Core\Db\Row $row) {
?Core\Time\Instant $paid = $row->instant('paid_at');
echo 'order ', $row->int('id') ?? 0, ': ', $paid == null ? 'not paid yet' : 'paid', "\n";
}
$db->close();
Output
order 1: paid
order 2: not paid yet

Show a payment on each shops clock

<?nvs
// Shows the moment each order was paid on the local clock of its shop.
// requires: postgres
var $db = Core\Db::connect('shop');
$db->execute('create temporary table orders (city text not null, zone text not null, paid_at timestamptz not null)', []);
$db->execute(
'insert into orders (city, zone, paid_at) values (?, ?, ?), (?, ?, ?)',
['London', 'Europe/London', '2026-03-01 14:00:00+00', 'New York', 'America/New_York', '2026-03-01 14:00:00+00'],
);
// Both orders were paid at the same moment. `in` returns that moment on the
// calendar and clock of a time zone. This prints `London: 14:00` and `New York: 09:00`.
foreach ($db->query('select city, zone, paid_at from orders order by city', []) as Core\Db\Row $row) {
?Core\Time\Instant $paid = $row->instant('paid_at');
if ($paid != null) {
Core\Time\DateTime $local = $paid->in(Core\Time\Zone::of($row->string('zone') ?? 'UTC'));
echo $row->string('city') ?? '', ': ', $local->format('HH:mm'), "\n";
}
}
$db->close();
Output
London: 14:00
New York: 09:00