![]() ![]() 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. DateTimeWidgetBase: Creates the basic UI and ensures that the date is using the correct timezone.DateTimeComputed: Calculate the date value into a format that can be displayed in the widget.DateTimeWidgetBase and DateTimeDefaultWidget work together to build a UI for the user to input and change the date value. Next we have the UI layer which builds a UI for the end user that can accept and change a date value. UI Image: Putting the date example into the date field DateTimeIso8601: Sets and gets datetimes in UTC and stores them in the ISO8601 format (YYYY-MM-DDTHH:MM:SS The time part is optional).DateTimeItem: Creates the field and its storage and differentiates between storing a datetime or just a date.All dates are stored in UTC in the database. When I save the value of Jat 1:45:00 pm into this field, DateTimeIso8601 calls the calculation layer to convert it to T20:45:00 and saves it in the database. When building the test date field Drupal invoked the DateTimeItem class which has two properties, a DateTimeIso8601 and a DateTimeComputed (we’ll come back to the computed field later). Let’s start at the storage layer, which sets up the field so that it can properly interact with the other layers. Storage Image: ISO time stored in the database To illustrate these transitions I’ve made a date field and saved it with the value of Jat 1:45:00 pm. The following section outlines each major component and the classes they use. Calculation: Sits underneath the rest, providing an easy means of changing between different ways of representing the date. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |