Download the PHP package icecave/chrono without Composer
On this page you can find all versions of the php package icecave/chrono. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package chrono
Chrono
Chrono is a PHP date & time library that is decoupled from the system clock.
composer require icecave/chrono
Rationale
Many date & time operations in the core PHP libraries require access to system state such as the current wall time, or resources such as timezone databases. These hard-wired dependencies can make it very difficult to write well-abstracted and testable code when dealing with time-sensitive operations.
Chrono provides a set of date & time classes that are completely decoupled from the system and hence behave consistently, regardless of system state and configuration (such as the date.timezone INI directive).
A SystemClock instance must be explicitly constructed before any global date & time operations are used. Classes that require use of a clock may take a ClockInterface as a dependency, improving decoupling and testability.
Concepts
- Clock: A factory for chronological measurements.
- Time: A chronological measurement with a time component.
- Date: A chronological measurement with a date component.
- Time Point: A discreet point on the time-continuum.
- Time Span: An un-anchored span of time.
- Interval: A span of time between two Time Points.
Implementations
- System Clock: A factory for chronological measurements that uses the system clock.
- Test Clock: A clock that can be manually manipulated for testing purposes.
- Date: Represents a date. Models the Time Point and Date concepts.
- Time of Day: Represents a time of day. Models the Time concept.
- Date Time: Represents a time of day on specific date. Models the Time Point, Date and Time concepts.
- Interval: A span of time between two Time Points. Models the Interval concept.
- Month: A one month time span. Models the Interval concept.
- Year: A one year time span. Models the Interval concept.
- Duration: A time span measured in seconds with no beginning or end. Models the Time Span concept.
- Period: A time span specified in component form (eg: 3 months, 4 days), models the Time Span concept.
Examples
Getting the current time
In order to get the current time you need to use a clock. Most of the time in production code you will use the SystemClock class, which uses the machine's current system time and time zone information.
Each of the clock methods shown above has a UTC counterpart. For example, to obtain the current time in UTC you can use the following code:
String formatting
To produce a formatted string representing a DateTime,
TimeZone instance use the format()
method.
The output is specified using the same format as PHP's built-in date() function.
Casting the object as a string (or calling isoString()
) produces an ISO-8601
string representation.
Unix timestamps
DateTime instances can be produced from unix
timestamps using the fromUnixTime()
static method. The unix timestamp can be retrieved using unixTime()
.
PHP native "DateTime" objects
DateTime instances can be produced from
native PHP DateTime instances using the fromNativeDateTime()
static
method, and can be converted to a native DateTime using nativeDateTime()
.