Download the PHP package vdhicts/time without Composer
On this page you can find all versions of the php package vdhicts/time. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package time
Short Description Time value-object with parser and collection and helpers for duration, rounding and ranges. Makes working with times easy.
License MIT
Homepage https://github.com/vdhicts/time
Informations about the package time
Time
This package aims to make working with times (as in date, ranges and/or duration) easier. It contains a time value-object, parser, ranges, duration, rounding, etc.
Requirements
This package requires PHP 8.1 or higher.
Installation
You can install the package via composer:
composer require vdhicts/time
Usage
This package aims to provide an easy usage, with some similarities with PHP's DateTime and Carbon. An example of that are the interfaces.
Time
The Time
object can be initiated with:
Building from input
To build the Time
object from several different inputs, you can use the following methods:
Comparison
The Time
object makes it easy to compare to other Time
objects. It offers the following methods:
Difference
The time object can also be used to calculate the difference between time objects:
The difference can be calculated in hours (diffInHours
), minutes (diffInMinutes
) and seconds (diffInSeconds
).
Duration
A time can be part of a date, i.e. 2022-03-01 10:00:00 but can also be used for a duration, i.e. "It took me 1 hour and 46 minutes to get there.".
Results in:
Rounding
Working with very specific times might not always be the result you want, for example for time tracking. This package allows you to round the time to your likings. By default, it rounds to 5 minutes:
It's also possible to round the seconds and/or change the precision. For example, round to 15 seconds:
Presentation
The Time
object can be presented as a string with the toString
method or just casting the object to a
string (string)$time
. This will output: 14:30:15
.
There are also two other presentations available, the toNumericalTime
and toReadableTime
:
Time collection
Time
objects can be collected in a TimeCollection
. The TimeCollection
ca be initiated with:
It's possible to provide an array of Time
objects or to use the add
and set
methods on the collection. The
contains
method provides the ability to check if the collection contains a Time
object.
Time range
A TimeRange
object contains two Time
objects, a start and end time. The TimeRange
object can be initiated with:
To get the duration of the range, you can get the Time
object as duration with:
Comparison
The TimeRange
object makes it easy to compare to a single Time
or another TimeRange
.
To determine if a Time
is in a range:
To determine if another range overlaps the range:
Tests
Unit tests are available in the tests folder. Run with:
composer test
When you want a code coverage report which will be generated in the build/report folder. Run with:
composer test-coverage
Contribution
Any contribution is welcome, but it should meet the PER 2.0 code style and please create one pull request per feature/bug. In exchange, you will be credited as contributor.
Security
If you discover any security related issues in this or other packages of Vdhicts, please email [email protected] instead of using the issue tracker.
License
This package is open-sourced software licensed under the MIT license