作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我希望能够在我们的条件下使用date字段在drupal 8中运行一些实体查询
听是我的密码:
$now = new DrupalDateTime('now');
$query = \Drupal::entityQuery('node');
$query->condition('field_date', $now->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=');
$results = $query->execute();
最佳答案
要解决此问题,我们需要在运行查询之前将时区设置为UTC。
$now = new DrupalDateTime('now');
$now->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
$timezone = drupal_get_user_timezone();
// drupal_get_user_timezone() returns for us the string representation of
// the timezone the current user has selected,or if they haven't,
// the site default timezone*/
$start = new \DateTime('now', new \DateTimezone($timezone));
$start->setTime(16,0);
$start->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
$start = DrupalDateTime::createFromDateTime($start);
$end = new \DateTime('now', new \DateTimezone($timezone));
$end->setTime(18, 0);
$end->setTimezone(new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
$end = DrupalDateTime::createFromDateTime($end);
$query = \Drupal::entityQuery('node');
$query
->condition('field_date', $start->format(DATETIME_DATETIME_STORAGE_FORMAT), '>=')
->condition('field_date', $end->format(DATETIME_DATETIME_STORAGE_FORMAT), '<=');
$results = $query->execute();
关于php - 如何在Drupal 8中使用日期查询实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57771847/
我是一名优秀的程序员,十分优秀!