s-mcdonald/chronicle

一个轻量级的PHP日期和时间处理工具

0.0.1 2023-05-10 12:50 UTC

This package is auto-updated.

Last update: 2024-09-16 10:20:30 UTC


README

Source Source

Chronicle 是一个日期和时间包,为PHP程序提供流畅的表达和功能来管理日期和时间。

Chronicle对象有一些常用的有用函数

Chronicle::isLeapYear(2028);                            // true
Chronicle::agoText($date1, $date2);                     // 3 day ago

日期对象仅代表日期,没有时间,因此没有时区组件。您可以使用流畅/链式命令表达您的日期,并且每个返回的值都是不可变的,并且是新的对象。

$today = Date::create(6,11, 2024);
$today->nextFortnight()->addDays(1)->getDayOfWeek();    // Thursday 

在某些应用程序中,比较当前月份和上个月可能很棘手。这是因为并非每个月都有相同的天数。Chronicle和Date对象都有一个方法可以轻松处理日期比较。

///
/// Period Shifting
///
$feb29 = Date::create(29,2, 2024);
$feb29->getSameDayLastMonth();                          // 29-01-2024
$feb29->getSameDayLastMonth(DateShiftRule::Business);   // 31-01-2024
$feb29->getSameDayLastMonth(DateShiftRule::Strict);     // 29-01-2024

$july31 = Date::create(31,7, 2024);
$july31->getSameDayLastMonth(DateShiftRule::PhpCore);   // 01-07-2024
$july31->getSameDayLastMonth(DateShiftRule::Business);  // 30-06-2024
$july31->getSameDayLastMonth(DateShiftRule::Strict);    // 30-06-2024

$today->greaterThan($date);                             // true
$today->shiftToFirstDayOfMonth();                       // 01-11-2024
$today->shiftToLastDayOfMonth();                        // 30-11-2024

时钟

echo "\n\n\nChronicle: Clock\n---------------------\n";
$clock = new Clock();

for ($i = 0; $i < 5; $i++) {
    sleep(1);
    echo $clock->getTime(), PHP_EOL;
}


echo "\n\n\nChronicle: FrozenClock\n---------------------\n";
$frozen = new FrozenClock($clock->getDateTimeImmutable());
for ($i = 0; $i < 5; $i++) {
    sleep(1);
    echo $frozen->getTime(), PHP_EOL;
}

生成以下输出

Chronicle: Clock
---------------------
12:21:07.758297
12:21:10.759012
12:21:13.759637
12:21:16.759783
12:21:19.760419


Chronicle: FrozenClock
---------------------
12:21:19.760695
12:21:19.760695
12:21:19.760695
12:21:19.760695
12:21:19.760695

时间(对象)

Chronicle还附带一个Time类。由于这是一个时间类,没有日期的概念,所以像Date对象一样,不需要时区。这纯粹是为了帮助显示和格式化时间以及在不同对象类型之间转换。

时间对象是不可变的。

$time = Time::create(3,15,20,500);
$time->getHour();                           // 3
$time->getMinute();                         // 15
$time->getSeconds();                        // 20
$time->getMicroseconds();                   // 500
$time->getUnixTimestamp();                  // -62170007072
$time->getTimestamp();                      // -62170007072
$time->toShortTimeString();                 // 03:15 AM
$time->toLongTimeString();                  // 03:15:20 AM
$time->toMySqlDateTimeString();             // 00-00-00 03:15:20

更多示例

Chronicle的大部分方法都处理当前日期或时间。因此,对于提供的值,示例是在`12-05-2023`上执行的。

///
/// Chronicle class
/// 
Chronicle::createDate(1,1,1969);                        // 01-01-1969
Chronicle::dateNow();                                   // 12-05-2023
Chronicle::dateLastWeek();                              // 05-05-2023
Chronicle::dateNextWeek();                              // 19-05-2023
Chronicle::dateTomorrow();                              // 13-05-2023
Chronicle::dateYesterday();                             // 11-05-2023
Chronicle::dateLastFortnight();                         // 28-04-2023
Chronicle::dateNextFortnight();                         // 26-05-2023

Chronicle::sameDayLastMonth();                          // 12-04-2023
Chronicle::sameDayLastMonth(DateShiftRule::Business);   // 12-04-2023
Chronicle::sameDayLastMonth(DateShiftRule::Strict);     // 12-04-2023

Chronicle::dayOfWeek();                                 // Friday    
Chronicle::monthOfYear();                               // May
Chronicle::agoText($date1, $date2);                     // 1 day ago

Chronicle::getWeekOfYear("2023-01-23");                 // 4
Chronicle::weekOfYear();                                // int value representing current week of the year

Chronicle::isLeapYear(2028);                            // true


///
/// Date (as created as 1/November/2024)
/// 
$today = Date::create(6,11, 2024);
$today->getDay();                                       // 6
$today->getMonth();                                     // 11
$today->getYear();                                      // 2024

$today->addDays(3);                                     // 09-11-2024
$today->subDays(9);                                     // 28-10/2024
$today->subMoths(1);                                    // 06-12-2024
$today->subMoths(1)->addMonths(2);                      // 06-12-2024 
$today->addYears(3);                                    // 06-11-2027
$today->subYears(9);                                    // 06-11-2015

$today->getTimestamp();                                 // 1730811600
$today->getUnixTimestamp();                             // 1730811600
$today->toMySqlDateTimeString();                        // 2024-11-06 00:00:00

$date = new DateTime("1/1/2005");
$today->greaterThan($date);                             // true
$today->shiftToFirstDayOfMonth();                       // 01-11-2024
$today->shiftToLastDayOfMonth();                        // 30-11-2024
$today->tomorrow();                                     // 07-11-2024
$today->yesterday();                                    // 05-11-2024
$today->lastWeek();                                     // 30-10-2024
$today->nextWeek();                                     // 13-11-2024
$today->lastFortnight();                                // 23-10-2024
$today->nextFortnight();                                // 20-11-2024
$today->getDayOfWeek();                                 // Wednesday
$today->lastFortnight()->getDayOfWeek();                // Wednesday
$today->nextFortnight()->tomorrow()->getDayOfWeek();    // Thursday
$today->nextFortnight()->addDays(1)->getDayOfWeek();    // Thursday 
$today->getWeekOfYear();                                // 45
$today->setYear(2024)->isLeapYear();                    // true

///
/// String formatting
/// 
$today->asYmd(DateSeperator::Dash);                     // 2024-11-06
$today->asYmd(DateSeperator::ForwardSlash);             // 2024/11/06
$today->toShortDateString();                            // 2024-11-06
$today->toUSShortDateString();                          // 11-06-2024
$today->toLongDateString();                             // Wednesday 6th of November 2024
$today->getDayOfWeek();                                 // Wednesday
$today->getMonthOfYear();                               // November
$today->getDayOfWeekInt();                              // 3
$today->toAbbreviatedDayMonthYearString();              // Wed, Nov 06, 2024
$today->toAbbreviatedMonthDayString();                  // Nov 6

///
/// Period Shifting
///
$feb29 = Date::create(29,2, 2024);
$feb29->getSameDayLastMonth();                          // 29-01-2024
$feb29->getSameDayLastMonth(DateShiftRule::Business);   // 31-01-2024
$feb29->getSameDayLastMonth(DateShiftRule::Strict);     // 29-01-2024

$july31 = Date::create(31,7, 2024);
$july31->getSameDayLastMonth(DateShiftRule::PhpCore);   // 01-07-2024
$july31->getSameDayLastMonth(DateShiftRule::Business);  // 30-06-2024
$july31->getSameDayLastMonth(DateShiftRule::Strict);    // 30-06-2024


///
/// Static methods
///
Date::createFromDateTimeInterface(new DateTime("now"));
Date::createFromTimestamp(string $timestamp);           // example Date::createFromTimestamp("1730811600");
Date::create(int $day, int $month, int $year);
Date::today();
Date::dateNextWeek();
Date::dateLastWeek();
Date::dateTomorrow();
Date::dateYesterday();
Date::dateLastFortnight();
Date::dateNextFortnight();
Date::validateDateString();

文档

安装

通过Composer。从您项目的根目录运行以下命令。

composer require s-mcdonald/chronicle

依赖

  • Php 8.0

许可

Chronicle根据MIT许可证(见LICENSE文件以获取详细信息)的条款进行许可。