Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit e03376f

Browse files
committed
docs: added DateTime rule
1 parent 0b972e8 commit e03376f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

docs/03-rules.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
## Date Rules
3333

34+
- [DateTime](03-rules_date-time.md)
3435
- [Timezone](03-rules_timezone.md)
3536

3637
## Choice Rules

docs/03-rules_date-time.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# DateTime
2+
3+
Validates that a given value is a valid datetime in a specific format.
4+
5+
```php
6+
DateTime(
7+
string $format = 'Y-m-d H:i:s',
8+
?string $message = null
9+
);
10+
```
11+
12+
## Basic Usage
13+
14+
```php
15+
// default "Y-m-d H:i:s"
16+
Validator::dateTime()->validate('2024-01-01 00:00:00'); // true
17+
Validator::dateTime()->validate('2024-01-01'); // false
18+
19+
// validate date
20+
Validator::dateTime(format: 'Y-m-d')->validate('2024-01-01'); // true
21+
Validator::dateTime(format: 'Y-m-d')->validate('2024-01-35'); // false
22+
23+
// validate time
24+
Validator::dateTime(format: 'H:i:s')->validate('21:00:00'); // true
25+
Validator::dateTime(format: 'H:i:s')->validate('35:00:00'); // false
26+
```
27+
28+
> [!NOTE]
29+
> An `UnexpectedValueException` will be thrown when the input value is not a `string` or an object implementing `\Stringable`.
30+
31+
## Options
32+
33+
### `format`
34+
35+
type: `string` default: `Y-m-d H:i:s`
36+
37+
Format of the datetime to be validated.
38+
Check all formatting options [here](https://www.php.net/manual/en/datetimeimmutable.createfromformat.php).
39+
40+
### `message`
41+
42+
type: `?string` default: `The {{ name }} value is not a valid datetime.`
43+
44+
Message that will be shown when the input value is not a valid datetime.
45+
46+
The following parameters are available:
47+
48+
| Parameter | Description |
49+
|----------------|---------------------------|
50+
| `{{ value }}` | The current invalid value |
51+
| `{{ name }}` | Name of the invalid value |
52+
| `{{ format }}` | The datetime format |
53+
54+
## Changelog
55+
56+
- `0.8.0` Created

0 commit comments

Comments
 (0)