|
| 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 | + |
| 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 | + |
| 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 | +``` |
0 commit comments