Skip to content

Commit 740de6d

Browse files
committed
docs: add more information regarding datetimes
1 parent 38c43d9 commit 740de6d

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

src/Web/Documentation/content/main/2-features/15-datetime.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 'Datetime'
3-
description: "Tempest provides a complete alternative to PHP's datetime implementation, with a better API, deeply integrated into Tempest."
3+
description: "Tempest provides a complete alternative to the DateTime implementation, with a higher-level API, deeply integrated into the framework."
44
keywords: ["timezone", "date", "time", "time zone"]
55
---
66

@@ -55,7 +55,7 @@ $date->minusDay();
5555
$date->endOfDay();
5656
```
5757

58-
## Converting timezones
58+
### Converting timezones
5959

6060
All datetime creation methods accept a `timezone` parameter. This parameter accepts an instance of the {b`Tempest\DateTime\Timezone`} enumeration. When not provided, the default timezone, provided by [`date.timezone`](https://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone), will be used.
6161

@@ -69,6 +69,49 @@ $date = DateTime::now();
6969
$date->convertToTimezone(Timezone::EUROPE_PARIS);
7070
```
7171

72+
### Computing a duration
73+
74+
By calling the `between()` method on a date instance, you may compute the duration between this date and a second one. This method returns a {b`Tempest\DateTime\Duration`} instance.
75+
76+
```php
77+
use Tempest\DateTime\DateTime;
78+
79+
$date1 = DateTime::now();
80+
$date2 = DateTime::parse('2025-09-19 02:00:00');
81+
$duration = $date1->between($date2);
82+
```
83+
84+
### Comparing dates
85+
86+
The {b`Tempest\DateTime\DateTime`} instance provides multiple methods to compare dates against each other, or against the current time. For instance, you may check if a date is before or after another date using the `isBefore()` and `isAfter()` methods, respectively.
87+
88+
```php
89+
// Check if a date is before another date, inclusively
90+
$date->isBefore($other);
91+
92+
// Check if a date is before another date, exclusively
93+
$date->isBeforeOrAtTheSameTime($other);
94+
95+
// Check if a date between two other dates, inclusively
96+
$date->betweenTimeInclusive($otherDate1, $otherDate2);
97+
98+
// Check if a date is in the future
99+
$date->isFuture();
100+
```
101+
102+
## Formatting dates
103+
104+
You may format a {b`Tempest\DateTime\DateTime`} instance in a specific format using the `format()` method. This method accepts an optional format string, which is a standard [ICU format](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax), and an optional locale.
105+
106+
```php
107+
use Tempest\DateTime\FormatPattern;
108+
use Tempest\DateTime\Locale;
109+
110+
$date->format(); // 19 Sept 2025, 02:00:00
111+
$date->format(pattern: FormatPattern::COOKIE); // Monday, 19-Sept-2025 02:00:00 BST
112+
$date->format(locale: Locale::FRENCH); // 19 sept. 2025, 02:00:00
113+
```
114+
72115
## Testing time
73116

74117
Tempest provides a time-related testing utilities accessible through the `clock` method of the [`IntegrationTest`](https://github.com/tempestphp/tempest-framework/blob/main/src/Tempest/Framework/Testing/IntegrationTest.php) test case.

0 commit comments

Comments
 (0)