|
| 1 | +# PHP Date Parser |
| 2 | + |
| 3 | +This library parses different strings to DateTime or DateTimeImmutable classes. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +composer require ixnode/php-date-parser |
| 9 | +``` |
| 10 | + |
| 11 | +```bash |
| 12 | +vendor/bin/php-date-parser -V |
| 13 | +``` |
| 14 | + |
| 15 | +```bash |
| 16 | +php-date-parser 0.1.0 (07-07-2023 22:23:01) - Björn Hempel <bjoern@hempel.li> |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```php |
| 22 | +use Ixnode\PhpDateParser\DateParser; |
| 23 | +``` |
| 24 | + |
| 25 | +### Date parser |
| 26 | + |
| 27 | +```php |
| 28 | +$dateParser = (new DateParser('<2023-07-01'))->formatFrom('Y-m-d H:i:s'); |
| 29 | +// null |
| 30 | +
|
| 31 | +$dateParser = (new DateParser('<2023-07-01'))->formatTo('Y-m-d H:i:s'); |
| 32 | +// 2023-06-30 23:59:59 |
| 33 | +``` |
| 34 | +
|
| 35 | +### Word parser |
| 36 | +
|
| 37 | +```php |
| 38 | +$dateParser = (new DateParser('today'))->formatFrom('Y-m-d H:i:s'); |
| 39 | +// 2023-07-07 00:00:00 |
| 40 | +
|
| 41 | +$dateParser = (new DateParser('today'))->formatTo('Y-m-d H:i:s'); |
| 42 | +// 2023-07-07 23:59:59 |
| 43 | +``` |
| 44 | +
|
| 45 | +## Parsing formats / overview |
| 46 | +
|
| 47 | +* Imagine today would be the 2023-07-07 |
| 48 | +
|
| 49 | +| string | description | from | to | |
| 50 | +|-----------------------------------------|-------------------------------------------------------|--------------------------------------|--------------------------------------| |
| 51 | +| <nobr>`"today"`</nobr> | Returns the date range from today. | <nobr>`"2023-07-07 00:00:00"`</nobr> | <nobr>`"2023-07-07 23:59:59"`</nobr> | |
| 52 | +| <nobr>`"yesterday"`</nobr> | Returns the date range from yesterday. | <nobr>`"2023-07-06 00:00:00"`</nobr> | <nobr>`"2023-07-06 23:59:59"`</nobr> | |
| 53 | +| <nobr>`"2023-07-01"`</nobr> | Exactly the given date. | <nobr>`"2023-07-01 00:00:00"`</nobr> | <nobr>`"2023-07-01 23:59:59"`</nobr> | |
| 54 | +| <nobr>`"<2023-07-01"`</nobr> | Lower than the given date (excluding the given one). | <nobr>`NULL`</nobr> | <nobr>`"2023-06-30 23:59:59"`</nobr> | |
| 55 | +| <nobr>`"<+2023-07-01"`</nobr> | Lower than the given date (including the given one). | <nobr>`NULL`</nobr> | <nobr>`"2023-07-01 23:59:59"`</nobr> | |
| 56 | +| <nobr>`">2023-07-01"`</nobr> | Higher than the given date (excluding the given one). | <nobr>`"2023-07-02 00:00:00"`</nobr> | <nobr>`NULL`</nobr> | |
| 57 | +| <nobr>`">+2023-07-01"`</nobr> | Higher than the given date (including the given one). | <nobr>`"2023-07-01 00:00:00"`</nobr> | <nobr>`NULL`</nobr> | |
| 58 | +| <nobr>`"+2023-07-01"`</nobr> | Alias of `">+2023-07-01"` | <nobr>`"2023-07-01 00:00:00"`</nobr> | <nobr>`NULL`</nobr> | |
| 59 | +| <nobr>`"2023-07-01\|2023-07-03"`</nobr> | Date from `"2023-07-01"` to `"2023-07-03"` | <nobr>`"2023-07-01 00:00:00"`</nobr> | <nobr>`"2023-07-03 23:59:59"`</nobr> | |
| 60 | +| <nobr>`"2023-07-01\|today"`</nobr> | Date from `"2023-07-01"` to `"today"` | <nobr>`"2023-07-01 00:00:00"`</nobr> | <nobr>`"2023-07-07 23:59:59"`</nobr> | |
| 61 | +| <nobr>`"2023-07-01\|yesterday"`</nobr> | Date from `"2023-07-01"` to `"yesterday"` | <nobr>`"2023-07-01 00:00:00"`</nobr> | <nobr>`"2023-07-06 23:59:59"`</nobr> | |
| 62 | +
|
| 63 | +## Methods |
| 64 | +
|
| 65 | +### Class `DateParser` |
| 66 | +
|
| 67 | +| method | description | type | |
| 68 | +|--------------------------------------|--------------------------------------------------------|---------------------------| |
| 69 | +| <nobr>`->formatFrom($format)`</nobr> | Returns the formatted "from" date. | `string` | |
| 70 | +| <nobr>`->formatTo($format)`</nobr> | Returns the formatted "to" date. | `string` | |
| 71 | +| <nobr>`->getDateRange()`</nobr> | Returns the range as `DateRange` class. | `DateRange` | |
| 72 | +| <nobr>`->getFrom()`</nobr> | Returns the "from" date as `DateTime` object. | `DateTime\|null` | |
| 73 | +| <nobr>`->getTo()`</nobr> | Returns the "to" date as `DateTime` object. | `DateTime\|null` | |
| 74 | +| <nobr>`->getFromImmutable()`</nobr> | Returns the "from" date as `DateTimeImmutable` object. | `DateTimeImmutable\|null` | |
| 75 | +| <nobr>`->getToImmutable()`</nobr> | Returns the "to" date as `DateTimeImmutable` object. | `DateTimeImmutable\|null` | |
| 76 | +
|
| 77 | +## Development |
| 78 | +
|
| 79 | +```bash |
| 80 | +git clone git@github.com:ixnode/php-date-parser.git && cd php-date-parser |
| 81 | +``` |
| 82 | +
|
| 83 | +```bash |
| 84 | +composer install |
| 85 | +``` |
| 86 | +
|
| 87 | +```bash |
| 88 | +composer test |
| 89 | +``` |
| 90 | +
|
| 91 | +## License |
| 92 | +
|
| 93 | +This tool is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details |
0 commit comments