定制 prokki/ext-datetime 二次开发

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

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

prokki/ext-datetime

Composer 安装命令:

composer require prokki/ext-datetime

包简介

Extends the common php DateTime objects with a lot of additional helpful methods.

README 文档

README

LICENSE Packagist LICENSE PHP v7.3 codecov Build Status

Extends the native datetime objects (\DateTime and \DateTimeImmutable) with a lot of additional helpful methods.

Most of the new methods are short cuts of already existing functionality. But these methods will help you to save time and code if you need to handle a many date- and time-operations.

All new methods support method chaining.

Table of Contents

Requirements

The usage of PHP v7.3 is obligatory.

Integration

Please install via composer.

composer require prokki/ext-datetime "^0.0"

Usage

The classes and instances of the classes ExtDateTime/DateTime and ExtDateTime/DateTimeImmutable can be used exactly like native datetime objects/classes.

Example

use ExtDateTime\DateTime;
use ExtDateTime\DateTimeImmutable;

// create a datetime object
$dateTime = new DateTime("now");

// create an immutable datetime object
$dateTimeImmutable = new DateTimeImmutable("now");

Static Initialization

Similar to the native objects there are several others ways to create a datetime object.

All static methods can be used to implement method chaining.

create()

Use the static constructor create() to use chaining immediately.

use ExtDateTime\DateTime;

// create a datetime object
// and use chaining immediately
$dateTime = DateTime::create("now")
            ->addHours(5)
            ->addDays(5)
            ;

current()

The static constructor current() returns a datetime object with the current date/time. But in opposite to DateTime::create("now") this method returns the object with additional microtime.

use ExtDateTime\DateTime;

$currentMicroseconds = DateTime::current()->format("u");         // output example 654321
$noMicroseconds      = DateTime::create("now")->format("u");     // output always 000000

createFromObject()

Creates a new object from any datetime object implementing the DateTimeInterface.

use ExtDateTime\DateTimeImmutable;

// create an immutable datetime object from a native non-immutable object
$datetime = DateTimeImmutable::createFromObject(new \DateTime());

Cloning

Two new methods add the availability to use chaining directly after cloning an object.

duplicate()

This method is just a wrapper function for clone:

use ExtDateTime\DateTime;

// create a datetime object
$datetime = DateTime::current();

// clone the datetime object and proceed like usual with chaining
$clone = $datetime->duplicate()
         ->addHours(5)
         ->addDays(5)
         ;

toImmutable() / toMutable()

Instead of using static constructors (DateTime::createFromImmutable or DateTimeImmutable::createFromMutable) you can also use these new non-static methods.

use ExtDateTime\DateTime;

// create a datetime object
$datetime = DateTime::current();

$clonedImmutable = $datetime->toImmutable();       // only available in class DateTime

$clonedMutable   = $clonedImmutable->toMutable();  // only available in class DateTimeImmutable

Manipulating

Most of the new methods are short cuts to avoid re-initialization of necessary parameter in your code.

addHours() / subHours()

Adds or subtracts hours of a datetime object.

use ExtDateTime\DateTime;

// create a datetime object and add 10 hours
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addHours(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 22:35:17"

// create a datetime object and subtracts 10 hours
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subHours(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 02:35:17"

addDays() / subHours()

Adds or subtracts days of a datetime object.

use ExtDateTime\DateTime;

// create a datetime object and add 10 days
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addDays(10)
                  ->format("Y-m-d h:i:s");                   // "2020-08-09 22:35:17"

// create a datetime object and subtracts 10 days
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subDays(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-20 02:35:17"

addMonth() / subMonth()

Adds or subtracts months of a datetime object.

Attention: This methods behaves differently than the suspected background function of adding days. If the current date is the last day of the month (31/30/29/28) and the target month has less days then the current month, the day will be set to the last day of the target month.

Example: The datetime object is

2017-01-30 17:00:00

and you want to add 1 month, the result will be

2017-02-28 17:00:00

use ExtDateTime\DateTime;

// create a datetime object and add 10 months
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addMonth(10)
                  ->format("Y-m-d h:i:s");                   // "2020-08-09 22:35:17"

// create a datetime object and subtracts 10 months
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subMonth(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-20 02:35:17"

Instant Setter Methods

toEndOfDay()

Sets the time to the end of the day (23:59:59).

use ExtDateTime\DateTime;

// create a datetime object and sets the time to the end of the day
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toEndOfDay(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 23:59:59"

toNoon()

Sets the time to noon (12:00:00).

use ExtDateTime\DateTime;

// create a datetime object and sets the time to noon
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toNoon(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 12:00:00"

toStartOfDay()

Sets the time to the start of the day (00:00:00).

use ExtDateTime\DateTime;

// create a datetime object and sets the time the start of the day
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toStartOfDay(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 00:00:00"

toStartOfMonth()

Sets the date to the first day of the month and additionally the time to the start of the day (00:00:00).

use ExtDateTime\DateTime;

// create a datetime object and sets the date to the first day of the month
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toStartOfMonth()
                  ->format("Y-m-d h:i:s");                   // "2020-07-01 00:00:00"

toEndOfMonth()

Sets the date to the last day of the month and additionally the time to the end of the day (23:59:59).

use ExtDateTime\DateTime;

// create a datetime object and sets the date to the last day of the month
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toEndOfMonth()
                  ->format("Y-m-d h:i:s");                   // "2020-07-31 23:59:59"

prokki/ext-datetime 适用场景与选型建议

prokki/ext-datetime 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 prokki/ext-datetime 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-31