|
| 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 | +#### Constructor parameters |
| 25 | + |
| 26 | +The `humandate` class constructor accepts the following parameters: |
| 27 | + |
| 28 | +- **`timestamp`** (int): The Unix timestamp to be rendered. |
| 29 | +- **`near`** (int|null): The number of seconds that indicates a nearby date. Defaults to `DAYSECS`, use `null` for no indication. Near dates will be rendered with a different styling depending on the theme (usually red with a warning icon). |
| 30 | +- **`timeonly`** (bool): Whether to show only the time or the full date and time. Defaults to `false`. |
| 31 | +- **`link`** (url|null): An optional URL to link the date to. |
| 32 | +- **`langtimeformat`** (string|null): An optional lang date and time format to use to format the date. Otherwise the output will use the user's preferences or the system default. |
| 33 | +- **`userelatives`** (bool): Whether to use human common words (tomorrow, yesterday) when possible. Defaults to `true`. |
| 34 | + |
| 35 | +### Example Usage |
| 36 | + |
| 37 | +```php title='This will output "Today" if the timestamp is for the current day.' |
| 38 | +use core_calendar\output\humandate; |
| 39 | + |
| 40 | +$renderer = $PAGE->get_renderer('core', 'output'); |
| 41 | +$timestamp = time() + HOURSECS; |
| 42 | + |
| 43 | +// Basic example. |
| 44 | +$humandate = new humandate($timestamp); |
| 45 | +echo $renderer->render($humandate); |
| 46 | + |
| 47 | +// Example adding a link to the date. |
| 48 | +$humandate = new humandate( |
| 49 | + timestamp: $timestamp, |
| 50 | + link: new core\url('/calendar/view.php', ['view' => 'day', 'time' => $timestamp]), |
| 51 | +); |
| 52 | +echo $renderer->render($humandate); |
| 53 | + |
| 54 | +// Example showing only the time. |
| 55 | +$humandate = new humandate( |
| 56 | + timestamp: $timestamp, |
| 57 | + timeonly: true, |
| 58 | +); |
| 59 | +echo $renderer->render($humandate); |
| 60 | +``` |
| 61 | + |
| 62 | +### `humantimeperiod` Class |
| 63 | + |
| 64 | +The `humantimeperiod` class is used to render a time period in a human-readable format. |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +#### Constructor parameters |
| 69 | + |
| 70 | +The `humantimeperiod` class constructor accepts the following parameters: |
| 71 | + |
| 72 | +- **`starttimestamp`** (int): The starting timestamp. |
| 73 | +- **`endtimestamp`** (int): The ending timestamp. |
| 74 | +- **`near`** (int|null): The number of seconds that indicates a nearby date. Defaults to `DAYSECS`, use `null` for no indication. |
| 75 | +- **`link`** (url|null): URL to link the date to. |
| 76 | +- **`langtimeformat`** (string|null): Lang date and time format to use to format the date. |
| 77 | +- **`userelatives`** (bool): Whether to use human common words or not. |
| 78 | + |
| 79 | +### Example Usage |
| 80 | + |
| 81 | +```php |
| 82 | +use core_calendar\output\humantimeperiod; |
| 83 | + |
| 84 | +$renderer = $PAGE->get_renderer('core', 'output'); |
| 85 | +$starttimestamp = time(); |
| 86 | +$endtimestamp = time() + HOURSECS; |
| 87 | + |
| 88 | +// Basic example. |
| 89 | +$humantimeperiod = new humantimeperiod($starttimestamp, $endtimestamp); |
| 90 | +echo $renderer->render($humantimeperiod); |
| 91 | + |
| 92 | +// Example adding a link to the date. |
| 93 | +$humantimeperiod = new humantimeperiod( |
| 94 | + starttimestamp: $starttimestamp, |
| 95 | + endtimestamp: $endtimestamp, |
| 96 | + link: new core\url('/calendar/view.php', ['view' => 'day', 'time' => $starttimestamp]), |
| 97 | +); |
| 98 | +echo $renderer->render($humantimeperiod); |
| 99 | +``` |
0 commit comments