stevegrunwell/time-constants
Composer 安装命令:
composer require stevegrunwell/time-constants
包简介
A series of constants designed to make it easier to express time in PHP applications
README 文档
README
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, \TimeConstants\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 * \TimeConstants\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 Cache facade.
Installation
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!
Defined constants
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.
As of version 2.0.0 of this library, all of these constants are defined in the TimeConstants namespace. If you are upgrading from version 1.x, please see the 2.0.0 release notes for notes about migration.
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.
Time based in seconds
ONE_SECOND(1 second)MINUTE_IN_SECONDS(60 seconds)HOUR_IN_SECONDS(3600 seconds)DAY_IN_SECONDS(86,400 seconds)WEEK_IN_SECONDS(604,800 seconds)MONTH_IN_SECONDS(2,592,000 seconds)YEAR_IN_SECONDS(31,536,000 seconds)
Time based in minutes
ONE_MINUTE(1 minute)HOUR_IN_MINUTES(60 minutes)DAY_IN_MINUTES(1,440 minutes)WEEK_IN_MINUTES(10,080 minutes)MONTH_IN_MINUTES(43,200 minutes)YEAR_IN_MINUTES(525,600 minutes)
Multipliers
These can be helpful when dealing with fractions of a second. For example, all of the following are equivalent:
6000 === (6 * ONE_SECOND) * 1000 === 6 * MILLISECONDS_PER_SECOND
MILLISECONDS_PER_SECOND(1,000ms/s)MICROSECONDS_PER_SECOND(1,000,000µs/s)NANOSECONDS_PER_SECOND(1,000,000,000ns/s)PICOSECONDS_PER_SECOND(1,000,000,000,000ps/s)
stevegrunwell/time-constants 适用场景与选型建议
stevegrunwell/time-constants 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 416.96k 次下载、GitHub Stars 达 28, 最近一次更新时间为 2018 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 stevegrunwell/time-constants 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stevegrunwell/time-constants 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 416.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 28
- 点击次数: 24
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-21