>condition('field_date', $end->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), 'execute() >condition('field_date', $start->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '>=') $end = DrupalDateTime::createFromDateTime($end) $end->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE)) $end = new \DateTime('now', new \DateTimezone($timezone)) $start = DrupalDateTime::createFromDateTime($start) $start->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE)) $start = new \DateTime('now', new \DateTimeZone($timezone)) So we can do something like this: $timezone = drupal_get_user_timezone() I prefer to work directly with \DateTime and then wrap it into the Drupal wrapper just because i can have all the native methods highlighted by my IDE. Let's say we want all the nodes with the time of today between 16:00 and 18:00 (a 2 hour span). However, we then convert it so that we can have them compared properly in the query.Ī more complex example could be a range of times. The subtle difference to understand is that we are creating $now totally relative to where we are (the site timezone) because we are interested in finding nodes in the future from us, not from from another timezone. $now->setTimezone(new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE)) Īnd then use $now in the query. To fix this, we need to set the timezone to UTC before running the query. And the further away from UTC we are, the more apples start to become compared to cars and airplanes. So if our timezone is not UTC, the query will suffer because we are essentially comparing apples with oranges. When creating the DrupalDateTime, the site default timezone is used. And the regular operators here allow us to find the right entities. Then we create our entity query and for the date field condition we pass the storage format so that it can be compared to what is being stored. First, we are using the Drupal wrapper of \DateTime and constructing an object to represent our current time. $query->condition('field_date', $now->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT), '>=') Ī few things to notice. Luckily though, we can, and should always in Drupal 8 try to rely on the entity.query service when looking for entities.įirst, an easy one: how do we query for all the nodes which have the field_date value in the future. Running a db_query() type of query for returning nodes with the date in a certain range would be a pain at best and impossible at worst. So we need to construct our node queries carefully if we want reliable results. However, the site timezone is rarely UTC and we very well may have users choosing their own timezones. By default in Drupal 8, the storage for this date is in the format Y-m-d\TH:i:s and the timezone is UTC. Imagine a simple date field on the Node entity which stores date and time. The requirement for returning entities which have a date field with a value between certain hours is definitely not an edge case, and although seems like an easy task, it can be tricky. In this article we are going to look at how we can run some entity queries in Drupal 8 using the Date field in our conditions. However, we always still have the complication of data storage, formatting and different timezones management. PHP 5.2 introduced the DateTimeInterface which makes handling dates, date ranges, intervals, comparisons etc much easier. Next time change is highlighted.ĭata for the years before 1970 is not available for Dallas, however, we have earlier time zone history for Chicago available.Dates have always been a tricky thing to manage in Drupal. The graph above illustrates clock changes in Dallas during 2023.ĭaylight Saving Time (DST) changes do not necessarily occur on the same date every year. Business Date to Date (exclude holidays).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |