Skip to content

Commit 5bb884f

Browse files
authored
Merge pull request #1 from stellarwp/feature/is-after-before
Add the is_after() and is_before() methods
2 parents 22fbfc9 + 5e707e8 commit 5bb884f

File tree

9 files changed

+156
-5
lines changed

9 files changed

+156
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.phpdoc/
12
files/
23
repo/
34
vendor/

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ composer require stellarwp/dates
5252
* [hour_only](/docs/classes/StellarWP/Dates/Dates.md#hour_only)
5353
* [immutable](/docs/classes/StellarWP/Dates/Dates.md#immutable)
5454
* [interval](/docs/classes/StellarWP/Dates/Dates.md#interval)
55+
* [is_after](/docs/classes/StellarWP/Dates/Dates.md#is_after)
56+
* [is_before](/docs/classes/StellarWP/Dates/Dates.md#is_before)
5557
* [is_between](/docs/classes/StellarWP/Dates/Dates.md#is_date_between)
5658
* [is_now](/docs/classes/StellarWP/Dates/Dates.md#is_now) - calls `is_between()` with a different parameter order, where the date to look for is last and optional.
5759
* [is_timestamp](/docs/classes/StellarWP/Dates/Dates.md#is_timestamp)

docs/classes/StellarWP/Dates/Date_I18n.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ Translated date.
7777

7878

7979
***
80-
> Automatically generated from source code comments on 2023-08-19 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)
80+
> Automatically generated from source code comments on 2024-09-11 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)

docs/classes/StellarWP/Dates/Date_I18n_Immutable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ Translated date.
7777

7878

7979
***
80-
> Automatically generated from source code comments on 2023-08-19 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)
80+
> Automatically generated from source code comments on 2024-09-11 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)

docs/classes/StellarWP/Dates/Dates.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,60 @@ The built date interval object.
928928

929929

930930

931+
***
932+
933+
### is_after
934+
935+
Is the given date after the date to compare?
936+
937+
```php
938+
public static is_after(string|\DateTimeInterface|int $date, string|\DateTimeInterface|int $date_to_compare): bool
939+
```
940+
941+
942+
943+
* This method is **static**.
944+
945+
946+
947+
948+
**Parameters:**
949+
950+
| Parameter | Type | Description |
951+
|-----------|------|-------------|
952+
| `$date` | **string|\DateTimeInterface|int** | |
953+
| `$date_to_compare` | **string|\DateTimeInterface|int** | |
954+
955+
956+
957+
958+
***
959+
960+
### is_before
961+
962+
Is the given date before the date to compare?
963+
964+
```php
965+
public static is_before(string|\DateTimeInterface|int $date, string|\DateTimeInterface|int $date_to_compare): bool
966+
```
967+
968+
969+
970+
* This method is **static**.
971+
972+
973+
974+
975+
**Parameters:**
976+
977+
| Parameter | Type | Description |
978+
|-----------|------|-------------|
979+
| `$date` | **string|\DateTimeInterface|int** | |
980+
| `$date_to_compare` | **string|\DateTimeInterface|int** | |
981+
982+
983+
984+
931985
***
932986

933987
### is_between
@@ -1831,4 +1885,4 @@ public static wp_locale_weekday(int|string $weekday, string $format = 'week
18311885

18321886

18331887
***
1834-
> Automatically generated from source code comments on 2023-08-19 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)
1888+
> Automatically generated from source code comments on 2024-09-11 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)

docs/classes/StellarWP/Dates/Timezones.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,4 +633,4 @@ Based on the core WP code found in wp-admin/options-general.php.
633633

634634

635635
***
636-
> Automatically generated from source code comments on 2023-08-19 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)
636+
> Automatically generated from source code comments on 2024-09-11 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ This is an automatically generated documentation for **Documentation**.
2626

2727

2828
***
29-
> Automatically generated from source code comments on 2023-08-19 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)
29+
> Automatically generated from source code comments on 2024-09-11 using [phpDocumentor](http://www.phpdoc.org/) and [saggre/phpdocumentor-markdown](https://github.com/Saggre/phpDocumentor-markdown)

src/Dates/Dates.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,32 @@ public static function interval( $interval_spec ): DateInterval {
803803
return $interval;
804804
}
805805

806+
/**
807+
* Is the given date after the date to compare?
808+
*
809+
* @since 1.1.3
810+
*
811+
* @param string|DateTimeInterface|int $date
812+
* @param string|DateTimeInterface|int $date_to_compare
813+
* @return boolean
814+
*/
815+
public static function is_after( $date, $date_to_compare ): bool {
816+
return static::get( $date ) > static::get( $date_to_compare );
817+
}
818+
819+
/**
820+
* Is the given date before the date to compare?
821+
*
822+
* @since 1.1.3
823+
*
824+
* @param string|DateTimeInterface|int $date
825+
* @param string|DateTimeInterface|int $date_to_compare
826+
* @return boolean
827+
*/
828+
public static function is_before( $date, $date_to_compare ): bool {
829+
return static::get( $date ) < static::get( $date_to_compare );
830+
}
831+
806832
/**
807833
* Determine if the given date is between two dates.
808834
*

tests/wpunit/Dates/IsTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,74 @@ public function tearDown() {
4949
Dates::$cache = [];
5050
}
5151

52+
public function get_date_after_dates() {
53+
yield 'date before comparison' => [
54+
'date' => '2024-01-01',
55+
'date_to_compare' => '2024-01-02',
56+
'expected' => false,
57+
];
58+
59+
yield 'date before comparison with time' => [
60+
'date' => '2024-01-01 09:59:59',
61+
'date_to_compare' => '2024-01-02 10:00:00',
62+
'expected' => false,
63+
];
64+
65+
yield 'date after comparison' => [
66+
'date' => '2024-01-05',
67+
'date_to_compare' => '2024-01-02',
68+
'expected' => true,
69+
];
70+
71+
yield 'date after comparison with time' => [
72+
'date' => '2024-01-02 10:00:00',
73+
'date_to_compare' => '2024-01-02 09:59:59',
74+
'expected' => true,
75+
];
76+
}
77+
78+
/**
79+
* @dataProvider get_date_after_dates
80+
* @test
81+
*/
82+
public function it_identifies_when_date_is_after_another_date( $date, $date_to_compare, $expected ) {
83+
$this->assertEquals( $expected, Dates::is_after( $date, $date_to_compare ) );
84+
}
85+
86+
public function get_date_before_dates() {
87+
yield 'date before comparison' => [
88+
'date' => '2024-01-01',
89+
'date_to_compare' => '2024-01-02',
90+
'expected' => true,
91+
];
92+
93+
yield 'date before comparison with time' => [
94+
'date' => '2024-01-01 09:59:59',
95+
'date_to_compare' => '2024-01-02 10:00:00',
96+
'expected' => true,
97+
];
98+
99+
yield 'date after comparison' => [
100+
'date' => '2024-01-05',
101+
'date_to_compare' => '2024-01-02',
102+
'expected' => false,
103+
];
104+
105+
yield 'date after comparison with time' => [
106+
'date' => '2024-01-02 10:00:00',
107+
'date_to_compare' => '2024-01-02 09:59:59',
108+
'expected' => false,
109+
];
110+
}
111+
112+
/**
113+
* @dataProvider get_date_before_dates
114+
* @test
115+
*/
116+
public function it_identifies_when_date_is_before_another_date( $date, $date_to_compare, $expected ) {
117+
$this->assertEquals( $expected, Dates::is_before( $date, $date_to_compare ) );
118+
}
119+
52120
public function get_date_between_dates() {
53121
yield 'date in middle of dates' => [
54122
'start' => '2020-01-01',

0 commit comments

Comments
 (0)