Skip to content

Commit 38c43d9

Browse files
committed
docs: document datetime
1 parent 07b5481 commit 38c43d9

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed
Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,87 @@
11
---
22
title: 'Datetime'
3-
description: ''
4-
hidden: true
3+
description: "Tempest provides a complete alternative to PHP's datetime implementation, with a better API, deeply integrated into Tempest."
4+
keywords: ["timezone", "date", "time", "time zone"]
55
---
6+
7+
## Overview
8+
9+
PHP provides multiple date and time implementations. There is [`DateTime`](https://www.php.net/manual/en/class.datetime.php) and [`DateTimeImmutable`](https://www.php.net/manual/en/class.datetimeimmutable.php), based on [`DateTimeInterface`](https://www.php.net/manual/en/class.datetimeinterface.php), as well as [`IntlCalendar`](https://www.php.net/manual/en/class.intlcalendar.php). Unfortunately, those implementation have rough, low-level, awkward APIs, which are not pleasant to work with.
10+
11+
Tempest provides an alternative to [`DateTimeInterface`](https://www.php.net/manual/en/class.datetimeinterface.php), partially based on [`IntlCalendar`](https://www.php.net/manual/en/class.intlcalendar.php). This implementation provides a better API with a more consistent interface. It was initially created by {x:azjezz} for the [PSL](https://github.com/azjezz/psl), and was ported to Tempest so it could be deeply integrated.
12+
13+
## Creating date instances
14+
15+
The {`Tempest\DateTime\DateTime`} class provides a `DateTime::parse()` method to create a date from a string, a timestamp, or another datetime instance. This is the most flexible way to create a date instance.
16+
17+
```php
18+
DateTime::parse('2025-09-19 02:00:00');
19+
```
20+
21+
Alternatively, if you know the format of the date string you are working with, you may use the `DateTime::fromPattern()`. It accepts a standard [ICU format](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax).
22+
23+
Finally, for more specific use cases, the `DateTime::fromString()` method may be used to create a date from a localized date and time string.
24+
25+
### For the current date and time
26+
27+
You may create a {b`Tempest\DateTime\DateTime`} instance for the current time using the `DateTime::now()` method or the `Tempest\now()` function.
28+
29+
```php
30+
$now = DateTime::now();
31+
```
32+
33+
### From a known format pattern
34+
35+
If you know the format of the date string you are working with, you should prefer using the `DateTime::fromPattern()` method. It accepts a standard [ICU format](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax).
36+
37+
```php
38+
DateTime::fromPattern('2025-09-19 02:00', pattern: 'yyyy-MM-dd HH:mm');
39+
```
40+
41+
## Manipulating dates
42+
43+
The {b`Tempest\DateTime\DateTime`} class provides multiple methods to manipulate dates. You may add or subtract time from a date using the `plus()` and `minus()` methods, which accept a {b`Tempest\DateTime\Duration`} instance.
44+
45+
For convenience, more specific manipulation methods are also provided.
46+
47+
```php
48+
// Adding a set duration
49+
$date->plus(Duration::seconds(30));
50+
51+
// Using convenience methods
52+
$date->plusHour();
53+
$date->plusMinutes(30);
54+
$date->minusDay();
55+
$date->endOfDay();
56+
```
57+
58+
## Converting timezones
59+
60+
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.
61+
62+
You may convert the timezone of an existing instance by calling the `convertToTimezone()` method:
63+
64+
```php
65+
use Tempest\DateTime\DateTime;
66+
use Tempest\DateTime\Timezone;
67+
68+
$date = DateTime::now();
69+
$date->convertToTimezone(Timezone::EUROPE_PARIS);
70+
```
71+
72+
## Testing time
73+
74+
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.
75+
76+
Calling this method replaces the {b`Tempest\Clock\Clock`} instance in the container with a testing one, on which a specific date and time can be defined. {b`Tempest\DateTime\DateTime`} instances created with the `DateTime::now()` method or `Tempest\now()` function will use the date and time specified by the testing clock.
77+
78+
```php
79+
// Create a testing clock
80+
$clock = $this->clock();
81+
82+
// Set a specific date and time
83+
$clock->setNow('2025-09-19 02:00:00');
84+
85+
// Advance time by the specified duration
86+
$clock->sleep(milliseconds: 250);
87+
```

0 commit comments

Comments
 (0)