Skip to content

Commit

Permalink
Merge pull request #1 from stellarwp/feature/is-after-before
Browse files Browse the repository at this point in the history
Add the is_after() and is_before() methods
  • Loading branch information
borkweb authored Sep 11, 2024
2 parents 22fbfc9 + 5e707e8 commit 5bb884f
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.phpdoc/
files/
repo/
vendor/
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ composer require stellarwp/dates
* [hour_only](/docs/classes/StellarWP/Dates/Dates.md#hour_only)
* [immutable](/docs/classes/StellarWP/Dates/Dates.md#immutable)
* [interval](/docs/classes/StellarWP/Dates/Dates.md#interval)
* [is_after](/docs/classes/StellarWP/Dates/Dates.md#is_after)
* [is_before](/docs/classes/StellarWP/Dates/Dates.md#is_before)
* [is_between](/docs/classes/StellarWP/Dates/Dates.md#is_date_between)
* [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.
* [is_timestamp](/docs/classes/StellarWP/Dates/Dates.md#is_timestamp)
Expand Down
2 changes: 1 addition & 1 deletion docs/classes/StellarWP/Dates/Date_I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ Translated date.


***
> 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)
> 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)
2 changes: 1 addition & 1 deletion docs/classes/StellarWP/Dates/Date_I18n_Immutable.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ Translated date.


***
> 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)
> 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)
56 changes: 55 additions & 1 deletion docs/classes/StellarWP/Dates/Dates.md
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,60 @@ The built date interval object.



***

### is_after

Is the given date after the date to compare?

```php
public static is_after(string|\DateTimeInterface|int $date, string|\DateTimeInterface|int $date_to_compare): bool
```



* This method is **static**.




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$date` | **string|\DateTimeInterface|int** | |
| `$date_to_compare` | **string|\DateTimeInterface|int** | |




***

### is_before

Is the given date before the date to compare?

```php
public static is_before(string|\DateTimeInterface|int $date, string|\DateTimeInterface|int $date_to_compare): bool
```



* This method is **static**.




**Parameters:**

| Parameter | Type | Description |
|-----------|------|-------------|
| `$date` | **string|\DateTimeInterface|int** | |
| `$date_to_compare` | **string|\DateTimeInterface|int** | |




***

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


***
> 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)
> 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)
2 changes: 1 addition & 1 deletion docs/classes/StellarWP/Dates/Timezones.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,4 @@ Based on the core WP code found in wp-admin/options-general.php.


***
> 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)
> 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)
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ This is an automatically generated documentation for **Documentation**.


***
> 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)
> 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)
26 changes: 26 additions & 0 deletions src/Dates/Dates.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,32 @@ public static function interval( $interval_spec ): DateInterval {
return $interval;
}

/**
* Is the given date after the date to compare?
*
* @since 1.1.3
*
* @param string|DateTimeInterface|int $date
* @param string|DateTimeInterface|int $date_to_compare
* @return boolean
*/
public static function is_after( $date, $date_to_compare ): bool {
return static::get( $date ) > static::get( $date_to_compare );
}

/**
* Is the given date before the date to compare?
*
* @since 1.1.3
*
* @param string|DateTimeInterface|int $date
* @param string|DateTimeInterface|int $date_to_compare
* @return boolean
*/
public static function is_before( $date, $date_to_compare ): bool {
return static::get( $date ) < static::get( $date_to_compare );
}

/**
* Determine if the given date is between two dates.
*
Expand Down
68 changes: 68 additions & 0 deletions tests/wpunit/Dates/IsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,74 @@ public function tearDown() {
Dates::$cache = [];
}

public function get_date_after_dates() {
yield 'date before comparison' => [
'date' => '2024-01-01',
'date_to_compare' => '2024-01-02',
'expected' => false,
];

yield 'date before comparison with time' => [
'date' => '2024-01-01 09:59:59',
'date_to_compare' => '2024-01-02 10:00:00',
'expected' => false,
];

yield 'date after comparison' => [
'date' => '2024-01-05',
'date_to_compare' => '2024-01-02',
'expected' => true,
];

yield 'date after comparison with time' => [
'date' => '2024-01-02 10:00:00',
'date_to_compare' => '2024-01-02 09:59:59',
'expected' => true,
];
}

/**
* @dataProvider get_date_after_dates
* @test
*/
public function it_identifies_when_date_is_after_another_date( $date, $date_to_compare, $expected ) {
$this->assertEquals( $expected, Dates::is_after( $date, $date_to_compare ) );
}

public function get_date_before_dates() {
yield 'date before comparison' => [
'date' => '2024-01-01',
'date_to_compare' => '2024-01-02',
'expected' => true,
];

yield 'date before comparison with time' => [
'date' => '2024-01-01 09:59:59',
'date_to_compare' => '2024-01-02 10:00:00',
'expected' => true,
];

yield 'date after comparison' => [
'date' => '2024-01-05',
'date_to_compare' => '2024-01-02',
'expected' => false,
];

yield 'date after comparison with time' => [
'date' => '2024-01-02 10:00:00',
'date_to_compare' => '2024-01-02 09:59:59',
'expected' => false,
];
}

/**
* @dataProvider get_date_before_dates
* @test
*/
public function it_identifies_when_date_is_before_another_date( $date, $date_to_compare, $expected ) {
$this->assertEquals( $expected, Dates::is_before( $date, $date_to_compare ) );
}

public function get_date_between_dates() {
yield 'date in middle of dates' => [
'start' => '2020-01-01',
Expand Down

0 comments on commit 5bb884f

Please sign in to comment.