nxmcz/date-time 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Testing Coverage Status Mutation testing badge License Latest Stable Version Total Downloads License

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 nxmcz/date-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 278
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-07