ixnode / php-date-parser
PHP日期解析器 - 该库可以将各种给定的日期和时间字符串解析为DateTime或DateTimeImmutable类,并返回时间范围。例如,可以非常出色地用于命令行参数和选项,以便进行数据库查询。
1.0.3
2023-07-22 15:34 UTC
Requires
- php: ^8.2
- adhocore/cli: ^v1.0.0
- ixnode/php-naming-conventions: ^0.1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.13
- ixnode/bash-version-manager: ^0.1.3
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- povils/phpmnd: ^3.0
- rector/rector: ^0.15.1
README
此库可以将各种给定的日期和时间字符串解析为DateTime或DateTimeImmutable类,并返回时间范围。例如,可以非常出色地用于命令行参数和选项,以便进行数据库查询。
示例/用法
use Ixnode\PhpDateParser\DateParser;
日期解析器(UTC
)
print (new DateParser('2023-07-01'))->formatFrom('Y-m-d H:i:s'); // 2023-07-01 00:00:00 print (new DateParser('2023-07-01'))->formatTo('Y-m-d H:i:s'); // 2023-07-01 23:59:59
单词解析器(UTC
)
- 假设现在的时间是:
2023-07-07 12:34:56
print (new DateParser('today'))->formatFrom('Y-m-d H:i:s'); // 2023-07-07 00:00:00 print (new DateParser('today'))->formatTo('Y-m-d H:i:s'); // 2023-07-07 23:59:59
时区相关的日期解析器
- 输入:
America/New_York
- 输出:
Europe/Berlin
/* Parses given date time from timezone America/New_York; Output to timezone Europe/Berlin */ print (new DateParser('<2023-07-01', 'America/New_York'))->formatFrom('Y-m-d H:i:s', 'Europe/Berlin'); // null /* Parses given date time from timezone America/New_York; Output to timezone Europe/Berlin */ print (new DateParser('<2023-07-01', 'America/New_York'))->formatTo('Y-m-d H:i:s', 'Europe/Berlin'); // 2023-07-01 05:59:59
使用DateRange
类进行操作
/* Parses given date time from timezone America/New_York */ $dateParser = (new DateParser('2023-07-01', 'America/New_York')); /* Sets default output to timezone Asia/Tokyo */ $dateRange = $dateParser->getDateRange('Asia/Tokyo'); print $dateRange->getFrom()?->format('Y-m-d H:i:s (e)'); // 2023-07-01 13:00:00 (Asia/Tokyo) print $dateRange->getTo()?->format('Y-m-d H:i:s (e)'); // 2023-07-02 12:59:59 (Asia/Tokyo)
解析格式
支持的关键词
概述
精确时间解析器(=datetime
)
- 假设现在的时间是:
2023-07-07 12:34:56
时间大于解析器(>from
)
- 假设现在的时间是:
2023-07-07 12:34:56
- “到”值是
NULL
- 1) - 排除指定的值
- 2) - 包含指定的值
时间小于解析器(<to
)
- 假设现在的时间是:
2023-07-07 12:34:56
- “从”值是
NULL
- 1) - 排除指定的值
- 2) - 包含指定的值
范围解析器(from|to
)
- 假设现在的时间是:
2023-07-07 12:34:56
无限范围解析器(NULL
)
方法
类 DateParser
安装
composer require ixnode/php-date-parser
vendor/bin/php-date-parser --version
0.1.10 (2023-07-21 21:39:44) - Björn Hempel <bjoern@hempel.li>
命令行工具
用于在命令行中快速检查给定的日期时间。
vendor/bin/php-date-parser pdt --timezone-input=America/New_York --timezone-output=Europe/Berlin "<2023-07-01"
Given date time range: "2023-07-01" (America/New_York > Europe/Berlin)
+----------------------------------------------------------+------------------+
| Value | Given |
+----------------------------------------------------------+------------------+
| Given date time range (America/New_York > Europe/Berlin) | 2023-07-01 |
| Timezone (input) | America/New_York |
| Timezone (output) | Europe/Berlin |
+----------------------------------------------------------+------------------+
Parsed from given input string (duration: 86400 seconds):
+------+-------------+---------------------+---------------------+
| Type | Format | UTC | America/New York |
+------+-------------+---------------------+---------------------+
| From | Y-m-d H:i:s | 2023-07-01 04:00:00 | 2023-07-01 00:00:00 |
| To | Y-m-d H:i:s | 2023-07-02 03:59:59 | 2023-07-01 23:59:59 |
+------+-------------+---------------------+---------------------+
Parsed output (duration: 86400 seconds):
+------+-------------+---------------------+---------------------+
| Type | Format | UTC | Europe/Berlin |
+------+-------------+---------------------+---------------------+
| From | Y-m-d H:i:s | 2023-07-01 04:00:00 | 2023-07-01 06:00:00 |
| To | Y-m-d H:i:s | 2023-07-02 03:59:59 | 2023-07-02 05:59:59 |
+------+-------------+---------------------+---------------------+
支持时区
开发
git clone git@github.com:ixnode/php-date-parser.git && cd php-date-parser
composer install
composer test
许可证
此工具受MIT许可证的许可 - 详细信息请参阅LICENSE文件。