Download the PHP package flying/date without Composer

On this page you can find all versions of the php package flying/date. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package date

date

DateTime generator with support for adjusting current time.

Purpose

The main purpose of the library is to assist testing application in time-sensitive scenarios without a need to adjust its code (besides switching to use library itself for creating dates in application).

It is relatively simple to test time-sensitive scenarios for unit tests, but for functional tests it is much harder. Of course, there are plenty of solutions:

This library aims to keep the number of required changes at a level comparable with other solutions. Required updates for the code are listed below and basically limited to change of \DateTime constructor methods into call to static method of the different class. It also simplifies testing cases when code execution took some time and this time change used somehow (think of performance tracking as an example).

General information

Whole API is exposed as a single \Flying\Date\Date class. API is provided as a set of static methods to make sure that they will be accessible from any part of code without a need to introduce any kind of additional dependencies.

Generated date objects are limited to \DateTimeImmutable. Upcoming ClockInterface from PSR-20 also provides only immutable dates.

Main API methods - adjust method.

Usage in application

In order to benefit from the ability to use time shifting, it is required to update parts of your code which creates new instances of the \DateTime or \DateTimeInterval objects.

To get \DateTime object it is required to convert obtained \DateTimeInterval object:

Creating objects for current time and default timezone

Creating objects for arbitrary time and / or timezone

Creating from string

Creating from format

Usage in tests

In order to test application behavior in different points of time, you need to do two things:

  1. Provide date adjustment value by calling \Flying\Date\Date::adjust() and pass either \DateTimeInterface that represents new target date or \DateInterval to define time shift relatively to the current time. Calling adjust() with no arguments removes date adjustment.
  2. Enable date adjustment by calling \Flying\Date\Date::allowAdjustment(true)

IMPORTANT! You should never use date adjustment in your application's code, ONLY IN TESTS! Use of it in application's code may result in unexpected behavior!

Even in tests, you should limit use of this feature to the tests which actually needs date adjustment functionality and disable it after doing the test.

Simplified configuration using attribute

In order to simplify use of the library and to ensure correct enabling / disabling of the date adjustment functionality in tests - it is possible to use AdjustableDate PHP attribute.

To enable use of the attribute you need to edit your PHPUnit configuration (phpunit.xml) and add test extension:

Then for test classes or (preferably) test methods that need to use adjustable date functionality you need to add #[AdjustableDate] attribute. It accepts optional configuration parameters:

Note on microseconds in date adjustment

For time shifting interval and adjusted dates, generated by the from methods microseconds part of the resulted date is forcibly set to zero. Date objects, which are generated without a date adjustment, are not affected.

It should be safe because date adjustment is meant to only be used for tests. Testing time shifts with microsecond precision on intervals less than a second is more reliable with use of the usleep(). For larger intervals include of microseconds may introduce difference of the whole second which may cause tests to break from time to time.

API methods

now

Signature:

Provides the date object for the current date. Either timezone, defined through setTimezone() or PHP default timezone is used.

In case if date adjustment is enabled - returned date will be adjusted by the defined time shifting interval.

from

Signature:

Create the date object for a given arbitrary point of time from provided date and timezone information. Either given timezone, timezone defined through setTimezone() or PHP default timezone is used. It is allowed to pass valid timezone name as timezone value.

In case if date adjustment is enabled - returned date will be adjusted by the defined time shifting interval.

fromFormat

Signature:

Create the date object for given date string, formatted using given format using given timezone information. Either given timezone, timezone defined through setTimezone() or PHP default timezone is used. It is allowed to pass valid timezone name as timezone value.

In case if date adjustment is enabled - returned date will be adjusted by the defined time shifting interval.

getTimezone

Signature:

Get timezone that is used for creating date objects.

setTimezone

Signature:

Defines (or resets) timezone that is used for creating date objects. It is allowed to pass valid timezone name as value.

adjust

Signature:

Defines (or resets) the time shifting interval that is used for date adjusting while creating date objects. It is allowed to define either absolute point of time by passing \DateTimeInterface instance or a time shift relative to the current point of time by passing \DateInterval object.

It is also allowed to pass date formats or date interval definitions.

getAdjustment

Signature:

Returns currently defined the time shifting interval that is used for date adjusting while creating date objects.

allowAdjustment

Signature:

Allows control of the status of date adjustment functionality.

isAdjustmentAllowed

Signature:

Returns current status of date adjustment functionality.

License

Library is licensed under MIT License.


All versions of date with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package flying/date contains the following files

Loading the files please wait ....