Core\Db\Row::time
$row->time(string $name): ?Core\Time\TimeOfDay$row->time( string $name, ): ?Core\Time\TimeOfDay
Reads one column of a row as a clock reading.
Core\Db\Row::time takes the name of a column and returns a Core\Time\TimeOfDay. That is an hour, a minute and a second, such as the hour a shop opens or a train leaves. It has no day and no time zone behind it, because a TIME column has neither. The same reading stands for every day.
The result is null when the column has no value in this row, so the type you get back is ?Core\Time\TimeOfDay. 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 TIME. A column that holds a calendar day is read with Core\Db\Row::date, and one that holds a day and a time together is read with Core\Db\Row::instant.
The examples below print the hour a shop opens, read a column that may have no value, and pick out the shops that are open at nine.
Parameters
$namestringThe column label, as the server described it.
Return value
Returns ?Core\Time\TimeOfDay. The time of day, or null for a NULL column.
Errors
LogicErrorThe row has no column with that name, or the column is not aTIME.
Examples
The examples below run with this configuration file.
# 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 hour a shop opens
<?nvs// Shows how to read a clock reading out of a row.
var $db = Core\Db::connect('notes');$db->execute('create table shops (name text not null, opens time not null)', []);$db->execute('insert into shops (name, opens) values (?, ?), (?, ?)', ['the bakery', '06:30:00', 'the tea shop', '09:00:00']);
// `time` reads the named column as a `Core\Time\TimeOfDay`, and `format` writes// that reading out. This prints `the bakery: 06:30` and `the tea shop: 09:00`.foreach ($db->query('select name, opens from shops order by opens', []) as Core\Db\Row $row) { ?Core\Time\TimeOfDay $opens = $row->time('opens'); if ($opens != null) { echo $row->string('name') ?? '', ': ', $opens->format('HH:mm'), "\n"; }}
$db->close();the bakery: 06:30the tea shop: 09:00A column that has no value
<?nvs// Shows what you get when the clock column has no value in this row.
var $db = Core\Db::connect('notes');$db->execute('create table shops (name text not null, opens time)', []);$db->execute('insert into shops (name, opens) values (?, ?), (?, ?)', ['the market stall', null, 'the tea shop', '09:00:00']);
// `time` returns `null` for a column with no value, so your program says what// to print instead. This prints `the market stall: no fixed hour`.foreach ($db->query('select name, opens from shops order by name', []) as Core\Db\Row $row) { ?Core\Time\TimeOfDay $opens = $row->time('opens'); echo $row->string('name') ?? '', ': '; if ($opens != null) { echo $opens->format('HH:mm'), "\n"; } else { echo "no fixed hour\n"; }}
$db->close();the market stall: no fixed hourthe tea shop: 09:00Find the shops open at nine
<?nvs// Shows how to find the rows whose hour has come. The reading 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 shops (name text not null, opens time not null, closes time not null)', []);$db->execute( 'insert into shops (name, opens, closes) values (?, ?, ?), (?, ?, ?), (?, ?, ?)', ['the bakery', '06:30:00', '13:00:00', 'the tea shop', '09:00:00', '18:00:00', 'the bookshop', '10:00:00', '19:00:00'],);
var $nine = Core\Time\TimeOfDay::at(9, 0);
// `compareTo` returns a number above zero when the reading is after the other// reading. This prints `the bakery` and `the tea shop`, and not the bookshop.foreach ($db->query('select name, opens, closes from shops order by opens', []) as Core\Db\Row $row) { ?Core\Time\TimeOfDay $opens = $row->time('opens'); ?Core\Time\TimeOfDay $closes = $row->time('closes'); if ($opens != null) { if ($closes != null) { if ($opens->compareTo($nine) <= 0 && $closes->compareTo($nine) > 0) { echo $row->string('name') ?? '', "\n"; } } }}
$db->close();the bakerythe tea shop