承接 ambroisemaupate/date-to-sentence 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

ambroisemaupate/date-to-sentence

Composer 安装命令:

composer require ambroisemaupate/date-to-sentence

包简介

A simple lexer to print human readable dates.

README 文档

README

A simple lexer to print human readable dates.

Build Status

Requires

  • PHP 7.4 min.
  • PHP-intl extension

Available lexers

  • English
  • French
  • German

Usage

composer require ambroisemaupate/date-to-sentence
use AM\Date2Sentence\EnglishDateLexer;

$lexer = new EnglishDateLexer();

$lexer->setDates([
     new DateTime('2017-06-01'),
     new DateTime('2017-06-02'),
     new DateTime('2017-06-03'),
 ]);

echo $lexer->toSentence();
// "From June 1st to June 3rd"


$lexer->setDates([
     new DateTime('2017-06-01'),
     new DateTime('2017-06-02'),
     new DateTime('2017-06-03'),
     new DateTime('2017-06-10'),
 ]);

echo $lexer->toSentence();
// "From June 1st to June 3rd and June 10th"

With wrap option

use AM\Date2Sentence\EnglishDateLexer;

$nonContinuousLexer = new EnglishDateLexer([
    new DateTime('2017-06-01'),
    new DateTime('2017-06-02'),
    new DateTime('2017-06-03'),
    new DateTime('2017-06-10'),
], ['wrap_format' => '<span>%s</span>']);

echo $nonContinuousLexer->toSentence();
// "From <span>June 1st</span> to <span>June 3rd</span> and <span>June 10th</span>"

With French lexer

use AM\Date2Sentence\FrenchDateLexer;

$lexer = new FrenchDateLexer([
    new DateTime('2017-06-01'),
    new DateTime('2017-06-02'),
    new DateTime('2017-06-03'),
]);

echo $lexer->toSentence();
// "Du 1er au 3 juin"

$nonContinuousLexer = new FrenchDateLexer([
    new DateTime('2017-06-01'),
    new DateTime('2017-06-02'),
    new DateTime('2017-06-03'),
    new DateTime('2017-06-10'),
]);

echo $nonContinuousLexer->toSentence();
// "Du 1er au 3 juin et le 10 juin"

Setting tolerance

Set tolerance before dates to changes how days are listed. You can force from-to writing even if there are some missing days (e.g. weekends).

use AM\Date2Sentence\EnglishDateLexer;

$nonContinuousLexer = new EnglishDateLexer();
// Tolerate 1 missing day between dates.
$nonContinuousLexer->setTolerance(1);
$nonContinuousLexer->setDates([
    new DateTime('2017-06-01'),
    // no 2nd
    new DateTime('2017-06-03'),
    // no 4th
    new DateTime('2017-06-05'),
    // no 6th
    new DateTime('2017-06-07'),
]);

echo $nonContinuousLexer->toSentence();
// "From June 1st to June 7th"

Grouping by month

Date2Sentence is made to group days within the same month not to repeat same month-name over and over.

use AM\Date2Sentence\EnglishDateLexer;

$nonContinuousLexer = new EnglishDateLexer();
$nonContinuousLexer->setDates([
    new DateTime('2017-06-01'),
    // no 2nd
    new DateTime('2017-06-03'),
    // no 4th
    new DateTime('2017-06-05'),
    // no 6th
    new DateTime('2017-06-07'),
    new DateTime('2017-07-01'),
]);

echo $nonContinuousLexer->toSentence();
// "June 1st, 3rd, 5th, 7th and July 1st"

//
// In French, it works too…
// "Les 1er, 2, 5, 7 juin et le 1er juillet"

Get hours

Date2Sentence can also extract times from your given dates:

use AM\Date2Sentence\EnglishDateLexer;

$lexer = new EnglishDateLexer();
$lexer->setDates([
    new DateTime('2017-06-01 20:00:00'),
    new DateTime('2017-06-03 21:00:00'),
    new DateTime('2017-06-05 20:00:00'),
    new DateTime('2017-06-07 21:00:00'),
    new DateTime('2017-07-01 20:00:00'),
]);

echo json_encode(array_keys($this->getLexer()->getAvailableTimes()));
// [
//    "20:00",
//    "21:00"
// ]

Get dates as array

If you need to render manually you date groups, you can use generic toArray method which will group start and end dates into arrays. Dates in same array are continuous, separate arrays mean that they’re not continuous.

use AM\Date2Sentence\EnglishDateLexer;

$lexer = new EnglishDateLexer();
$lexer->setDates([
    new DateTime('2017-06-01 20:00:00'),
    new DateTime('2017-06-02 21:00:00'),
    new DateTime('2017-06-03 21:00:00'),
    new DateTime('2017-07-01 20:00:00'),
    new DateTime('2017-07-02 21:00:00'),
    new DateTime('2017-07-03 21:00:00'),
]);

echo json_encode($this->getLexer()->toArray());
// [
//    [
//        {"date":"2017-06-01 20:00:00.000000","timezone_type":3,"timezone":"Europe\/Paris"},
//        {"date":"2017-06-03 21:00:00.000000","timezone_type":3,"timezone":"Europe\/Paris"}
//    ],
//    [
//        {"date":"2017-07-01 20:00:00.000000","timezone_type":3,"timezone":"Europe\/Paris"},
//        {"date":"2017-07-03 21:00:00.000000","timezone_type":3,"timezone":"Europe\/Paris"}
//    ]
// ]

Get days of week

Date2Sentence can also extract days of week from your given dates, day will be represented as their number (1 for Monday, 7 for Sunday) and will be ordered:

use AM\Date2Sentence\EnglishDateLexer;

$lexer = new EnglishDateLexer();
$lexer->setDates([
    new DateTime('2017-06-01'),
    new DateTime('2017-06-02'),
    new DateTime('2017-06-03'),
    new DateTime('2017-06-08'),
    new DateTime('2017-06-08'),
]);

echo json_encode($this->getLexer()->getAvailableDaysOfWeek());
// [
//    4,
//    5,
//    6
// ]

Tests

vendor/phpunit/phpunit/phpunit --bootstrap vendor/autoload.php test

ambroisemaupate/date-to-sentence 适用场景与选型建议

ambroisemaupate/date-to-sentence 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.93k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 ambroisemaupate/date-to-sentence 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-27