A series of constants designed to make it easier to express time in PHP applications
The Time Constants package defines a number of useful PHP constants within your application, making it easier to express measures of time.
For example, assume we have a
cache() function that accepts three arguments:
- The cache key.
- The value to cache.
- The amount of time (in seconds) to cache the value.
Currently, caching a value for 24 hours might look something like this:
cache($cacheKey, $value, 24 * 60 * 60);
Instead of spending the time figuring out what
24 * 60 * 60 means (or the fact that
86400 is meant to be one day in seconds), Time Constants allows you to represent the time using an easy-to-understand PHP constant:
cache($cacheKey, $value, DAY_IN_SECONDS);
If the requirements changed and we needed to cache the value for multiple days, we can rewrite it as:
cache($cacheKey, $value, 5 * DAY_IN_SECONDS);
These constants may seem familiar to WordPress developers, as they're absolutely inspired by WordPress' use of time constants. This package goes a bit further, however, adding
*_IN_MINUTES constants, for easier use with libraries like Laravel's
Time Constants is intended to be installed via Composer:
$ composer require stevegrunwell/time-constants
The package has been configured to automatically expose the
constants.php file via the Composer-generated autoloader, so you'll be ready to use the constants as soon as the package has been installed!
This is a list of all constants defined by this package, along with their values. Each constant is wrapped in a
if (! defined(...)) conditional, ensuring these constants can easily be redefined if necessary and won't conflict with existing constants.
Please note that these constants are defined for convenience, and not necessarily for accuracy; all months are treated as 30 days, and years as 365 days. If you need support for leap years or more advanced measures of time, you might consider PHP's
DateTimeclass or Nesbot's Carbon package.