ixnode / php-date-parser
PHP Date Parser - This library parses various given date and time strings into DateTime or DateTimeImmutable classes which return the time range. Can be used e.g. excellently for command line arguments and options to make database queries with.
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
This library parses various given date and time strings into DateTime or DateTimeImmutable classes which return the time range. Can be used e.g. excellently for command line arguments and options to make database queries with.
Examples / Usage
use Ixnode\PhpDateParser\DateParser;
Date parser (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
Word parser (UTC
)
- Imagine that now is the time:
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
Date parser with timezones
- Input:
America/New_York
- Output:
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
Working with DateRange
class
/* 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)
Parsing formats
Supported words
Overview
Exact time parser (=datetime
)
- Imagine that now is the time:
2023-07-07 12:34:56
Time is greater than parser (>from
)
- Imagine that now is the time:
2023-07-07 12:34:56
- "To" values are
NULL
- 1) - excluding the given one
- 2) - including the given one
Time is less than parser (<to
)
- Imagine that now is the time:
2023-07-07 12:34:56
- "From" values are
NULL
- 1) - excluding the given one
- 2) - including the given one
Range parser (from|to
)
- Imagine that now is the time:
2023-07-07 12:34:56
Infinitive range parser (NULL
)
Methods
Class DateParser
Installation
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>
Command line tool
Used to quickly check a given date time directly in the command line.
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 |
+------+-------------+---------------------+---------------------+
Supported timezones
See: src/Constants/Timezones.php
Development
git clone git@github.com:ixnode/php-date-parser.git && cd php-date-parser
composer install
composer test
License
This tool is licensed under the MIT License - see the LICENSE file for details