定制 glucnac/datetimeinterval 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

glucnac/datetimeinterval

Composer 安装命令:

composer require glucnac/datetimeinterval

包简介

DateTimeInterval is wrapper of DateInterval that provides useful methods for calculating delays.

README 文档

README

Source Code Latest Version PHP Version Pipeline status Coverage Status Total Downloads

When you want to compute a delay between two DateTimes, it can be quickly tricky to get the result you are expecting for, and sometimes you need to perform some calculations, which can lead to incorrect result.

DateTimeInterval is wrapper of DateInterval that provides useful methods for calculating delays. Thanks to this library, there is no need to compute results returned from DateInterval anymore. Moreover, all of its methods are fully tested, and are explicitly named and commented.

In that way, you don't have to copy/paste the logic you implemented on another project, just install this library and enjoy.

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require glucnac/datetimeinterval

Usage

$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Absolute count of days
echo $dateTimeInterval->getDays(); // 31

// Relative count of days
echo $dateTimeInterval->getDays(false); // 0 (15th day of the month (august) - 15th day of the month (july))
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2023-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Absolute count of months
echo $dateTimeInterval->getMonths(); // 13

// Relative count of months
echo $dateTimeInterval->getMonths(false); // 1 (8th month of the year (august) - 7th month of the year (july))

And so on :

$dateTimeInterval->getYears();
$dateTimeInterval->getHours();
$dateTimeInterval->getMinutes();
$dateTimeInterval->getSeconds();
$dateTimeInterval->getMicroseconds();

Result can be signed or unsigned, depending on the third parameter of the constructor ($returnAbsoluteValue, default is true).

$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate, false);

echo $dateTimeInterval->getDays(); // -31

Result can be formatted as a string :

$supDate = new DateTime('2021-08-27 00:00:00');
$infDate = new DateTime('2022-09-29 23:35:59');
$dateTimeInterval = new DateTimeInterval($firstDate, $secondDate);

// Internally uses DateInterval::format() method
echo $dateTimeInterval->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds'); // 1 years, 1 months, 2 days, 23 hours, 35 minutes, 59 seconds

Nostalgic about DateInterval ? No problem, you can get it back :

$dateInterval = $dateTimeInterval->getDateInterval();

What is the purpose ?

To understand the usefulness of this library, let's try to compute some delays.

Get delays using date_diff()

// Let's say you want to compute some delays between 2022-07-15 and 2022-08-15.

$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2022-08-15');

$dateInterval = date_diff($secondDate, $firstDate);
// OR
$dateInterval = $firstDate->diff($secondDate);

// You want to count the days between these two dates. Using DateInterval,
// you notice that two properties are available :

// PhpDoc states: "Totals number of days in the interval span"
$dateInterval->days;

// PhpDoc states: "Number of days"
$dateInterval->d;

// Well, the doc of ->d is not really clear, let's see the difference between these two returns

echo $dateInterval->days; // 30
echo $dateInterval->d; // 0

// Nice, it seems ->d is a relative count, whereas ->days give an absolute count

// What about getting the number of months then ?

// PhpDoc states: "Number of months"
echo $dateInterval->m // 1

// We don't yet if it's a relative or an absolute count. So let's change the year to next one :
$firstDate = new DateTimeImmutable('2022-07-15');
$secondDate = new DateTimeImmutable('2023-08-15');
$dateInterval = date_diff($secondDate, $firstDate);

echo $dateInterval->m // 1

// Now it's clear: ->m also returns a relative count

Actually: ->y, ->m, ->d, ->h, ->i, ->s, ->f: all are returning a relative count, which means that for any absolute result, you must perform some calculations using ->days, which is the only result to be absolute. Even though these calculations are simple, rewriting them on each of your project is boring and can lead to inattention mistakes.

That's why this library has been made for: to abstract the calculations and to get a more object-oriented coding.

Copyright and License

The GlucNAc/DateTimeInterval library is copyright © GlucNAc and licensed for use under the MIT License (MIT). Please see LICENSE for more information.

glucnac/datetimeinterval 适用场景与选型建议

glucnac/datetimeinterval 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.28k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「time」 「date」 「datetime」 「interval」 「DateTime operation」 「date_diff」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 glucnac/datetimeinterval 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 glucnac/datetimeinterval 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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