Time Type
TimeStringType
The TimeStringType can be used to represent a specific string field for HH:MM:SS times. It keeps the string type, but makes sure the format is normalized and valid.
Besides string input, it can also accept the framework dropdown select values from a default time control:
$data = [
// ...
'closing_time' => [
'hour' => '1',
'minute' => '12',
'second' => '20',
],
];
$entity = $this->Table->newEntity($data);
// $entity->closing_time is now 01:12:20To set it up for a specific field:
// In your Table class
protected function _initializeSchema(Table $table) {
$table->columnType('closing_time', 'time');
// ...
return $table;
}Then make sure you mapped the type to a class in your config:
// In your bootstrap
use Cake\Database\Type;
Type::map('time', 'Shim\Database\Type\TimeStringType');If you want to disable 24:00:00 as the valid upper boundary (and transform it to 00:00:00 instead):
// In your bootstrap
\Shim\Database\Type\TimeStringType::$normalizeUpperBoundary = true;What about objects?
We could also think about a value object representing time. This way you could offer a bit more functionality:
format()with, for example, onlyHH:MMetc. — but most of this can already be done on the presentation layer via a helper.diff()inHH:MM:SSor\DateInterval.isSame()/isBefore()/isAfter()methods.fromDateTime()and similar to extract the time part.
Even custom localized formatting like AM/PM could be added, but this should really be kept in the presentation layer to keep such logic out of the value object itself.
TIP
Feel free to add a TimeObjectType class for this use case.