ruslanovich111/date-converter
Composer 安装命令:
composer require ruslanovich111/date-converter
包简介
Convert dates
README 文档
README
The library provides extended functionality for working with dates.
- converting date rows "The 13th of Mar. 1999", "Mar. 2022" to objects.
- converting date rows in different languages "8 März 2023"
- converting date intervals "13th of Mar. 1999 - 8 März 2023"
- converting date sequences "The 12th of April 2022; 8 März 2023"
- search dates in a text in your language...
The library solves all these problems.
Versions
- There is code for php8.1 in the master branch (tags 1.0.*)
- There is code for php8.0 in the master-php8.0 branch (tags 1.1.*)
Configuration
Create ArrayPatternStorage.
The object takes config array of a strictly defined format:
- The
patternsfield should contain regular expressions for dates. - The
monthsfield should contain regular expressions for months. There should be exactly 12 of them.
<?php
$patternStorage = new ArrayPatternStorage([
'patterns' => [
'($day)th ($month) ($year)',
'The ($day)th of ($month) ($year)',
],
'months' => [
1 => ["January", "Jan\."],
2 => ["February", "Febr\."],
3 => ["March", "Mar\."],
4 => ["April"],
5 => ["May"],
6 => ["June"],
7 => ["July"],
8 => ["August"],
9 => ["September"],
10 => ["October"],
11 => ["November"],
12 => ["December"]
],
]);
The library automatically substitutes regular expressions:
$day = [0-9]{1,2}, $year = [0-9]{4}, $month = January|Jan\.|February|....
Convertation
All possible examples of using the library are in the examples folder.
For example, in ./examples/simple_date/test.php the simple convertation is described:
/** $patternStorage defined above */
$patternStorage = ...
$converter = new RowToModelConverter($patternStorage);
$converter->convertToDate("12th February 2022");
The convertToDate method will return a DateModel object since the string "12th February 2022" matches the first pattern in
configuration file.
If you pass a string that does not match any of the patterns, then an Exception will be thrown.
The order of the patterns in the configuration is important because the convertToDate method returns a DateModel based on the first matching pattern!
Advanced options
Interval convertation: interval-convertation
Sequence convertation: secuence-convertation
Multiple pattern storages: multiple-storages
Search
The library provides two search strategies:
- search with replacement
- search with repetitions
Examples of using search are described in the folder examples/search.
Next, let's explain the differences.
Let the input row be given:
It was sunny outside on the 12th of April 2022, 13 April 2022 and the 17th of April 2021
Search with replacement
- find all dates for the input corresponding to the first pattern, replace with $pattern_1, save the transformed row:
It was sunny outside on $pattern_1, 13 April 2022 and $pattern_1.
The dates for pattern 1 are stored in the TextAllPatternMatches model. - In the transformed row (with $pattern_1) we find the dates for the pattern 2, replace it with $pattern_2, save the transformed row:
It was sunny outside on $pattern_1, $pattern_2 and $pattern_1.
The dates for pattern 2 are stored in the TextAllPatternMatches model. - etc...
For this strategy, the order of the patterns in the configuration is important!
Search with repetitions
- find all dates for the input corresponding to the first pattern, replace with $pattern_1, save the transformed row:
It was sunny outside on $pattern_1, 13 April 2022 and $pattern_1.
The dates for pattern 1 are stored in the TextPattern model. - again we take the input row and find the dates corresponding to pattern 2, replace it with $pattern_2, save string:
It was sunny outside on the 12th of April 2022, $pattern_2 and the 17th of April 2021,
The dates for pattern 2 are stored in the TextPattern model. - etc...
Thus the TextPatternSequence is fill with the TextPattern models. The order is not important for this strategy.
Advanced options
Interval search: interval-search
ruslanovich111/date-converter 适用场景与选型建议
ruslanovich111/date-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.66k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ruslanovich111/date-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ruslanovich111/date-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2022-09-26