Skip to content

Commit 8109c04

Browse files
committed
[docs] Add information about new human date renderers
1 parent d249554 commit 8109c04

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
43.4 KB
Loading
43.9 KB
Loading
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: Date and Time Output Classes
3+
tags:
4+
- Output
5+
---
6+
7+
<Since version="5.0" issueNumber="MDL-83873" />
8+
9+
The `humandate` and `humantimeperiod` classes in Moodle are designed to render timestamps and time periods in a human-readable format. These classes provide functionality to display dates as "Today", "Yesterday", "Tomorrow", and apply alert styling if the date is near the current date.
10+
11+
## Using human time representation output classes
12+
13+
Both classes can be used as a normal output class in Moodle. Each class represent way of show dates and time in a human readable way:
14+
15+
- `humandate`: This renderer presents single dates and times in a user-friendly format, automatically adapting to the user's preferences and with some extra customization options.
16+
- `humantimeperiod`: Designed for displaying date/time ranges, this renderer optimizes information presentation, eliminating redundant date information and representing time in a more user friendly way.
17+
18+
### `humandate` Class
19+
20+
The `humandate` class is used to render a single timestamp as a human-readable date.
21+
22+
![inplace editable example.png](./_humandate/humandate_example.png)
23+
24+
### Example Usage
25+
26+
```php title='This will output "Today" if the timestamp is for the current day.'
27+
use core_calendar\output\humandate;
28+
29+
$renderer = $PAGE->get_renderer('core', 'output');
30+
$timestamp = (\core\di::get(\core\clock::class))->time();
31+
32+
// Basic example.
33+
$humandate = humandate::create_from_timestamp($timestamp);
34+
echo $renderer->render($humandate);
35+
36+
// Example adding a link to the date.
37+
$humandate = humandate::create_from_timestamp(
38+
timestamp: $timestamp,
39+
link: new core\url('/calendar/view.php', ['view' => 'day', 'time' => $timestamp]),
40+
);
41+
echo $renderer->render($humandate);
42+
43+
// Example showing only the time.
44+
$humandate = humandate::create_from_timestamp(
45+
timestamp: $timestamp,
46+
timeonly: true,
47+
);
48+
echo $renderer->render($humandate);
49+
```
50+
51+
### `humantimeperiod` Class
52+
53+
The `humantimeperiod` class is used to render a time period in a human-readable format.
54+
55+
![inplace editable example.png](./_humandate/humandate_example.png)
56+
57+
### Example Usage
58+
59+
```php
60+
use core_calendar\output\humantimeperiod;
61+
62+
$renderer = $PAGE->get_renderer('core', 'output');
63+
$starttimestamp = (\core\di::get(\core\clock::class))->time();
64+
$endtimestamp = $starttimestamp + HOURSECS;
65+
66+
// Basic example.
67+
$humantimeperiod = humantimeperiod::create_from_timestamp($starttimestamp, $endtimestamp);
68+
echo $renderer->render($humantimeperiod);
69+
70+
// Example adding a link to the date.
71+
$humantimeperiod = humantimeperiod::create_from_timestamp(
72+
starttimestamp: $starttimestamp,
73+
endtimestamp: $endtimestamp,
74+
link: new core\url('/calendar/view.php', ['view' => 'day', 'time' => $starttimestamp]),
75+
);
76+
echo $renderer->render($humantimeperiod);
77+
```

docs/devupdate.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ The course navigation has been updated to include a new link to the activity ove
1717

1818
See the [Activity overview page integration](./apis/plugintypes/mod/courseoverview) documentation for more information.
1919

20+
## Calendar: New human date renderers
21+
22+
<Since version="5.0" issueNumber="MDL-83873" />
23+
24+
To improve date readability and user experience, the Calendar subsystem is transitioning from deprecated date rendering methods, such as `calendar_format_event_time` or `calendar_time_representation`, to a new suite of human date renderers. It enhances date display by:
25+
26+
- Using relative terms like "Today," "Yesterday," and "Tomorrow" for nearby dates.
27+
- Applying alert styling to dates nearing deadlines or events.
28+
29+
This update introduces two primary renderers:
30+
31+
- `humandate`: This renderer presents single dates and times in a user-friendly format, automatically adapting to the user's preferred 12-hour or 24-hour time display (`CALENDAR_TF_12`/`CALENDAR_TF_24`).
32+
- `humantimeperiod`: Designed for displaying date/time ranges, this renderer optimizes information presentation. When the start and end dates fall on the same day, it shows the full start date and time, but only the end time, eliminating redundant date information. This logic mirrors the existing functionality of the deprecated functions, ensuring a consistent user experience.
33+
34+
See the [Date and Time Output Classes](./apis/subsystems/output/humandate.md) documentation for more information.
35+
2036
## Course formats
2137

2238
<Since version="5.0" issueNumber="MDL-83527" />

0 commit comments

Comments
 (0)