Skip to content

Repository files navigation

upyx/php-time

Source CodeLatest VersionSoftware LicensePHP VersionBuild StatusCoverage StatusTotal Downloads

Object oriented representation of time with microseconds. It like o'clock time without date, timezone or something else but cyclical for 24 hours.

Why not DateTime/DateTimeImmutable from standard library?

It is for different purposes. DateTime uses for calendar (linear) time through ages. But this library is intended to represent periodical time day by day.

Let's look closer. Noon in Tokyo and noon in New York are different times in the world. Noon on the 1st of December and noon on the 2nd December are different times. But noon is still 12 o'clock.

It is still possible to use DateTime for o'clock time, but it has some disadvantages. Firstly, it is curly because it needs some "zero-date". Secondary, it is difficult to compare and calculate because of changing dates. Thirdly, it is buggy because of timezone conversions. Fourthly, it confuses with the DateTime type used as calendar time.

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require upyx/php-time

Usage

LocalTime can be created in three ways:

useUpyx\PhpTime\LocalTime;
$time1 = newLocalTime(10, 20, 30, 40000);
$time2 = LocalTime::fromDateTime(newDateTimeImmutable('10:20:30'));
$time3 = LocalTime::fromMicroseconds(3600000000);

There are "add" and "subtract" methods:

useUpyx\PhpTime\LocalTime;
$time1 = newLocalTime(10, 0);
$time2 = newLocalTime(13, 0);
var_dump($time1->cyclicAdd($time2)); // 23:00:00.000000var_dump($time2->cyclicSubtract($time1)); // 03:00:00.000000var_dump($time1->cyclicSubtract($time2)); // 21:00:00.000000

As time is periodical, there are two distances between two times in both directions. There is a method to calculate the smallest distance between ones:

useUpyx\PhpTime\LocalTime;
$time1 = newLocalTime(2, 0);
$time2 = newLocalTime(12, 0);
$time3 = newLocalTime(22, 0);
var_dump($time1->calcDistance($time2)); // 10:00:00.000000var_dump($time2->calcDistance($time3)); // 10:00:00.000000var_dump($time1->calcDistance($time3)); // 04:00:00.000000

It is possible to use comparison operators. The smallest value is "00:00:00.000000", the greatest one is "23:59:59.999999".

useUpyx\PhpTime\LocalTime;
$time1 = newLocalTime(10, 0);
$time2 = newLocalTime(13, 0);
var_dump($time1 < $time2); // truevar_dump($time1 <= $time2); // truevar_dump($time1 > $time2); // falsevar_dump($time1 >= $time2); // falsevar_dump($time1 <=> $time2); // -1

Contributing

If you have a question, feel free to create an issue. If you would like to send me a pull request, please create an issue first.

Copyright and License

The upyx/php-time library is copyright © Sergey Rabochiy and licensed for use under the MIT License (MIT). Please see LICENSE for more information.

About

Object oriented representation of time and time only (without date, timezone or something else).

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages