matt-harvey/civil-date-time
Composer 安装命令:
composer require matt-harvey/civil-date-time
包简介
Civil date and time library for PHP
README 文档
README
a civil date and time library for PHP
Motivation
A civil date, or civil time, is a date/time without any timezone specified.
Such an entity should not be used for representing a precise, absolute moment or period in time. However, it is a good way to represent the concept of a "calendar date" or "clock time" as it is used in many everyday contexts.
For example, if someone was born on 1 January 2000, then they will typically want to celebrate their birthday on 1 January each year, in whichever timezone they happen to be living in at the time. In other words, the entity "1 January 2000", insofar as it represents their date of birth, is timezone-agnostic.
In a software context, this might be important if, for example, we want to send an email to each user on their birthday, where each user may have a different timezone associated with them. A given user's timezone might change over time; but their date of birth will not. It is with the user that the timezone should be associated here—not with their date of birth.
While it's possible to use the standard library's DateTime or DateTimeImmutable to represent
civil dates and times in PHP, it isn't ideal, as it requires an out-of-band convention about how to
interpret the timezone information attached to such an object. (Should the timezone data merely be
ignored? Should the date time be converted to UTC and then have its timezone ignored? Or should
non-UTC DateTimes be considered invalid as representations of civil dates?)
A dedicated class, that omits timezone information by design, allows civil dates, times, and date-time pairings, to be represented cleanly and directly.
For additional concrete use cases for civil dates, times, and date-times, see this comment in relation to a similar library proposed for Go.
Installation
composer require matt-harvey/civil-date-time
Note this library is still in a pre-v1 state, and there may be breaking changes in any release. (Although, I will generally avoid making breaking changes in patch version releases.)
Usage
There are three classes offered:
CivilDateCivilTimeCivilDateTime
Each of these is immutable: methods such as CivilDate::addDays() always return new
instances, rather than mutating the existing one.
Civil dates
use MattHarvey\CivilDateTime\CivilDate; // 15 March 2022 new CivilDate(2022, 3, 15); // or... CivilDate::fromIsoDateStamp('2022-03-15'); // format - signature mirrors \DateTime::format CivilDate::fromIsoDateStamp('2022-03-15')->format('D j M Y'); // 'Sat 15 Mar 2022' // convenience method for ISO format CivilDate::fromIsoDateStamp('2022-03-15')->toIsoDateStamp(); // 2022-03-15 // or just CivilDate::fromIsoDateStamp('2022-03-15')->__toString(); // 2022-03-15 // converting from standard library \DateTimeInterface to CivilDate // e.g., the moment that is 1:30pm in UTC on 22 Jan. 2021, falls on 23 January 2021 in Sydney $dateTime = new DateTimeImmutable('2021-01-22 13:30:01+0'); $sydney = new DateTimeZone('Australia/Sydney'); CivilDate::forMomentInTimezone($dateTime, $sydney); // 23 January 2021 // immutable addition/subtraction of days CivilDate::fromIsoDateStamp('2022-03-15')->addDays(3); // 18 March 2022 CivilDate::fromIsoDateStamp('2022-03-15')->addDays(-3); // 12 March 2022 // difference in days $dayA = CivilDate::fromIsoDateStamp('2010-03-05'); $dayB = CivilDate::fromIsoDateStamp('2010-04-05'); CivilDate::diffDays($dayB, $dayA); // 31 CivilDate::diffDays($dayA, $dayB); // -31 // comparison $dayB->laterThan($dayA); // true // extracting components $dayA->getYear(); // 2010 $dayA->getMonth(); // 3 $dayA->getDay(); // 5
Civil times
use MattHarvey\CivilDateTime\CivilTime; $civilTime = new CivilTime(22, 11, 18); // 10:11:18 p.m. CivilTime::from24HoursStamp('22:11:18'); // 10:11:18 p.m. CivilTime::from12HourClock(10, 11, 18, CivilTime::PM); // 10:11:18 p.m. $civilTime->get24Hour(); // 22 $civilTime->get12Hour(); // 10 $civilTime->getMinute(); // 11 $civilTime->getSecond(); // 18 $civilTime->getAmPm(); // 'pm' $civilTime->to24HourStamp(); // '22:11:18' $civilTimeB = new CivilTime(22, 11, 19); $civilTimeB->laterThan($civilTime); // true
Civil date/times
use MattHarvey\CivilDateTime\CivilTime; use MattHarvey\CivilDateTime\CivilDate; use MattHarvey\CivilDateTime\CivilDateTime; $civilDate = new CivilDate(2022, 3, 15); $civilTime = new CivilTime(22, 11, 18); $civilDateTime = new CivilDateTime($civilDate, $civilTime); // 10:11:18 p.m. on 15 Mar. 2022 CivilDateTime::fromIsoDateTimeStamp('2022-03-15T22:11:18'); $sydney = new DateTimeZone('Australia/Sydney'); $civilDateTime = new DateTimeImmutable('2021-06-26 20:34:05+10'); CivilDateTime::forMomentInTimezone($dateTime, $sydney); // 26 Jun. 2021, 8:34:05pm // convert back $civilDateTime->toDateTimeImmutable('Australia/Sydney'); // 26 Jun. 2021, 8:34:05pm, Australia/Sydney // or $civilDateTime->toDateTimeImmutable('Australia/Perth'); // 26 Jun. 2021, 8:34:05pm, Australia/Perth
Civil date/time in other PHP libraries
- https://github.com/brick/date-time offers the
LocalDate,LocalTimeandLocalDateTimeclasses, that are analogous tocivil-date-time'sCivilDate,CivilTimeandCivilDateTime. It also offers classes for modelling many other date/time concepts, whichcivil-date-timedoesn't do—includingZonedDateTimeas a replacement for the standard libraryDateTimeImmutable. You might prefer it tocivil-date-timeif you're looking for a more extensive set of date/time utilities, rather than a minimal adjunct to the standard library to plug just the civil date/time gap. - https://github.com/cakephp/chronos offers a
Dateclass, which is notionally similar tocivil-date-time'sCivilDate; however it extendsDateTimeImmutableand so carries a lot of redundant data as well as no-op functions inherited from the latter.
Civil date/time libraries in other programming languages
Contributing
PRs, bug reports, and suggestions are all welcome. Please ensure unit test coverage is maintained.
You will need xdebug installed to generate the coverage report.
composer test runs the test suite.
matt-harvey/civil-date-time 适用场景与选型建议
matt-harvey/civil-date-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 496 次下载、GitHub Stars 达 12, 最近一次更新时间为 2022 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「date」 「civil」 「civil date」 「civil time」 「civil date time」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 matt-harvey/civil-date-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 matt-harvey/civil-date-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 matt-harvey/civil-date-time 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
A powerful event calendar Tool for Laravel's Nova 5.
Date component is a set of methods to help with the manipulation of dates.
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Delayed Process For Laravel.
统计信息
- 总下载量: 496
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 13
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-07