PHP code example of elstc / cakephp-time-interval

1. Go to this page and download the library: Download elstc/cakephp-time-interval library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elstc / cakephp-time-interval example snippets


use Cake\Database\Schema\TableSchema;

class WorkTimesTable extends Table
{
    protected function _initializeSchema(TableSchema $schema)
    {
        parent::_initializeSchema($schema);

        $schema->setColumnType('duration', 'time_interval');

        // If your column type is seconds as INTEGER, Use `time_interval_int` instead.
        $schema->setColumnType('duration_sec', 'time_interval_int');

        return $schema;
    }
}

use Cake\Validation\Validator;
use Elastic\TimeInterval\Validation\TimeIntervalValidation;

class WorkTimesTable extends Table
{
    public function validationDefault(Validator $validator)
    {
        // ...
        $validator->add('duration', 'timeInterval', [
            'rule' => 'timeInterval',
            'provider' => 'timeInterval',
        ]);

        return $validator;
    }
}

use Cake\Database\Type;

class WorkTime extends Entity
{
    protected function _setDuration($value)
    {
        // convert to TimeInterval
        return Type::build('time_interval')->marshal($value);
    }
}

$workTime->duration = '00:15:00';
$workTime->duration = ($startTime)->diff($endTime); // $startTime, $endTime is FrozenTime object.
$workTime->duration = 3600; // as a seconds

$workTime->duration = new DateInterval('PT75H4M5S'); // OK
$workTime->duration = new DateInterval('P1M2DT3H4M5S'); // can't get expected time

$this->addPlugin('Elastic/TimeInterval');