PHP code example of vdhicts / time

1. Go to this page and download the library: Download vdhicts/time 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/ */

    

vdhicts / time example snippets


$time = new Time(14, 30, 15);

TimeFactory::createFromString('14:30:15'); // Time object with 14 hours, 30 minutes and 15 seconds
TimeFactory::createFromDateTime(new \DateTime('2023-01-01 14:30:15')); // Time object with 14 hours, 30 minutes and 15 seconds
TimeFactory::createFromTimestamp(1640000000); // Time object with 11 hours, 33 minutes and 20 seconds
TimeFactory::createFromDurationInSeconds(9000); // Time object with 2 hours and 30 minutes
TimeFactory::createFromDurationInMinutes(150); // Time object with 2 hours and 30 minutes

$time->isEqualTo(Time $anotherTime);
$time->isBefore(Time $anotherTime);
$time->isBeforeOrEqualTo(Time $anotherTime);
$time->isAfter(Time $anotherTime);
$time->isAfterOrEqualTo(Time $anotherTime);

$timeStart = new Time(10, 30);
$timeEnd = new Time(14);

$timeStart->diffInHours($timeEnd); // 3.5
$timeEnd->diffInHours($timeStart); // -3.5

$time = new Time(1, 46);

sprintf('It took me %s hours', $time->durationInHours());
sprintf('It took me %s minutes', $time->durationInMinutes());
sprintf('It took me %s seconds', $time->durationInSeconds());

$time = new Time(2, 46, 23);

$time->roundNatural(); // 02:45:00
$time->roundUp(); // 02:50:00
$time->roundDown(); // 02:45:00

$time = new Time(2, 21, 33);

$time->roundNatural(15, true); // 02:21:30
$time->roundUp(15, true); // 02:21:45
$time->roundDown(15, true); // 02:21:30

$time = new Time(12, 30, 45);
$time->toNumericalTime(); // 12:50
$time->toNumericalTime(true); // 12:50:75

$time->toReadableTime(); // 12:30
$time->toReadableTime(true); // 12:30:45

$timeCollection = new TimeCollection();

$timeRange = new TimeRange($time, $anotherTime);

$timeRange->getRangeDuration();

$time = new Time(14, 30, 15);
$timeRange->inRange($time);

$anotherTimeRange = new TimeRange($time, $anotherTime);
$timeRange->isOverlapping($anotherTimeRange);