![]() Always using datetime objects creates cleaner and more flexible code than just the standard strtotime and date functions (see the cheatsheet below for examples). Storing the data in a consistent format makes it easier for multiple different systems and endpoints to manipulate it because they can all assume the same starting point. These are all good practices to emulate in your own code. This is really useful if you need to do some more calculation on the template layer. ![]() Also, by default the formatters output both the converted date (Tue, - 13:45) AND the datetime value that it’s derived from (). Second, all of the above functionality depends on the PHP Datetime class wrapped by DateTimePlus and DrupalDateTime. First, the data is stored consistently (ISO format, UTC time, no am/pm) and then adjusted on the fly for the end user depending on their timezone and preferred format. ![]() Image: How it all works together What did we learn?ĭissecting the core date field reveals a number of things about how dates work and how we can navigate them when writing our own custom code. Namely integrating Drupal translation into the date format and pulling Drupal’s timezone system into the timezone calculation. DrupalDateTime: Extends DateTimePlus to add Drupal-specific requirements.DateTimePlus: Wraps PHP’s DateTime class so that it can create dates from more parameters (date object, timestamp, etc) and adds error handling.DateTimeItemInterface: Defines default values (timezone, datetime format, date format) that are needed for creating DrupalDateTime objects.DateTimeDefaultFormatter: Takes the ISO8601 formatted date from the base class and converts it to whatever format the field widget expects.įinally we have the calculation layer which is called by the other three layers to help smoothly translate data between them.It then converts the stored date from UTC to the correct timezone and outputs a render array of that data (still in ISO8601 format). This is normally the user’s timezone but it allows for overrides as well. DateTimeFormatterBase: Gets the correct timezone to display the date.DateTimeDefaultFormatter and DateTimeFormatterBase work together to output a properly formatted render array. Then, we come to rendering which displays the properly formatted date to the end user. DateTimeDefaultWidget: Extends the previous class and dictates what date and time the elements the UI will display for the user. ![]() Uses the DateTimezone base PHP class to do so.
0 Comments
Leave a Reply. |