nxmcz/date-time
Composer 安装命令:
composer require nxmcz/date-time
包简介
An DateTime class to deal with timestamp/DateTimeInterface
README 文档
README
An immutable class to deal with DateTime object used in Noxem systems. Works perfectly with nette/forms as Date, DateTime form's field.
Requirements
This library requires PHP 8.4 or later.
Usage
Noxem\DateTime\DT
Basic initialization of DT object. DT object is child of native DateTimeImmutable object with handles for modify date/time parts.
use Noxem\DateTime\DT; use DateTime as NativeDateTime; DT::create('now'); // 2022-08-07 12:00:00 (new DT('now')); // 2022-08-07 12:00:00 DT::create(1659866400); // 2022-08-07 12:00:00 initialize with timestamp DT::create('2022-08-07 12:00:00'); // 2022-08-07 12:00:00 DT::createFromParts(2022, 8, 7, 12); // 2022-08-07 12:00:00 DT::createFromFormat(); // PHP's native method DT::createFromInterface(new NativeDateTime()); DT::fromUTC("2022-08-07T12:00:00Z"); // 2022-08-07 14:00:00 in Europe/Prague DT::getOrCreateInstance("2022-08-07 12:00:00"); // 2022-08-07 12:00:00 DT::getOrCreateInstance(DT::create("2022-08-08 12:00:00")); // 2022-08-08 12:00:00 $dt = DT::create('now'); // 2022-08-07 12:00:00 $dt->modify('+5 seconds'); // 2022-08-07 12:00:05 $dt->addDays(1); // 2022-08-08 12:00:00 $dt->subDays(1); // 2022-08-06 12:00:00 $dt->modifyDays(1); // ekvivalent to addDays(1)
Library supports casting object into HTML's native input types
use Noxem\DateTime\DT; $object = DT::create('2022-08-07 12:00:00'); $object->toHtmlDate(); // 2022-08-07 $object->toHtmlMonth(); // 2022-08 $object->toHtmlWeek(); // 2022-W31 $object->toHtmlYear(); // 2022
Comparation:
DT::create('2022-08-07 12:00:00') ->areEquals( DT::create('2022-08-07 12:00:00') ->setTimezone('America/New_York') ); // FALSE $ny = DT::create("2020-09-17 07:00:00")->assignTimezone("America/New_York"); $tokyo = DT::create("2020-09-17 20:00:00")->assignTimezone("Asia/Tokyo"); $ny->areEquals($tokyo); // TRUE
isFuture(): bool We operating with future?
$dt = DT::create('now'); echo $dt->modify('+1 seconds')->isFuture(); // TRUE echo $dt->isFuture(); // FALSE
Noxem\DateTime\Difference
Difference formula can be imagined as x = a - b, where a is object which calling child method, formula example is x = $b->difference($a)
Difference class is accessible with method:
DT::create()->difference(DT $suspect)
$bigger = DT::create('2022-05-20 11:45:00'); $smaller = DT::create('2022-05-13 11:45:00'); $dt = $smaller->difference($bigger); echo $dt->hours(); // 168.0 echo $dt->days(); // 7 echo $dt->solidWeeks(); // 1 echo $dt->minutes(); // 1440.0 echo $dt->msec(); // 86400000 $dt = $bigger->difference($smaller); echo $dt->hours(); // -168.0 echo $dt->days(); // -7 echo $dt->solidWeeks(); // -1 echo $dt->minutes(); // -1440.0 echo $dt->msec(); // -86400000
Output of class is signed numbers. Where: positive numbers represent future, negative going back to future.
| Sign of number | Example | Description |
|---|---|---|
| - | -5 | Past |
| + | +5 | Future |
Method withAbsolute() ignores negative numbers on methods, difference will be always in positive numbers
$first = DT::create('2022-05-20 11:45:00'); $last = DT::create('2022-05-13 11:45:00'); $dt = $first->difference($last); echo $dt->hours(); // -168.0 echo $dt->withAbsolute()->hours(); // +168.0
Method is also immutable.
Noxem\DateTime\Overlap
Next method is for compare two objects if overlap or not.
use Noxem\DateTime\Overlapping; Overlapping::withTouching( DT::create('2021-05-06 09:00:00'), DT::create('2021-05-06 10:00:00'), DT::create('2021-05-06 10:00:00'), DT::create('2021-05-06 13:00:00'), ); // FALSE
Overlapping class handles only with one method (in future quantities increase). Description of interval overlap statements are presented in table below:
| Step | Interval visualisation | Result |
|---|---|---|
| BORDERS | I I |
|
| After | ██████ I I |
FALSE |
| Start touching | ███████I I |
FALSE |
| Start inside | ████I██ I |
TRUE |
| Inside start touching | I███████████I███████ |
TRUE |
| Enclosing start touching | I██████ I |
TRUE |
| Enclosing | I █████ I |
TRUE |
| Enclosing end touching | I ████I |
TRUE |
| Exact match | I███████████I |
TRUE |
| Inside | ████I███████████I██████ |
TRUE |
| Inside end touching | ████I███████████I |
TRUE |
| End inside | I ████I████████████ |
TRUE |
| End touching | I I████████████ |
FALSE |
| Before | I I ███████████ |
FALSE |
Exception
Bad DateTime format throws an exception which is children of InvalidArgumentException
use Noxem\DateTime\DT; use Noxem\DateTime\Exception\BadFormatException; $dt = DT::create('foo'); // BadFormatException
nxmcz/date-time 适用场景与选型建议
nxmcz/date-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 278 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「immutable」 「date-time」 「date time」 「dt」 「noxem」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nxmcz/date-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nxmcz/date-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nxmcz/date-time 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP port of the Java JSR310 time API
Simple, immutable data structures
A comprehensive collection of PHP utility functions for array manipulation, string operations, date handling, HTML generation, form building, validation, and more. Modern PHP 8.2+ library with full type safety.
Tools to convert between .NET and PHP date formats
A powerful event calendar Tool for Laravel's Nova 5.
Date component is a set of methods to help with the manipulation of dates.
统计信息
- 总下载量: 278
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-07