A
A
Alexey2017-03-02 15:38:01
PHP
Alexey, 2017-03-02 15:38:01

Bitrix how to implement a filter by custom field?

Good afternoon!
Faced the problem of filtering HighloadBlock by date range in Bitrix.
How can I filter by date range?
Code example (does not filter):

use Bitrix\HighloadBlock as HL;
use Bitrix\Main\Entity;

CModule::IncludeModule('highloadblock');

$hlbl = 2;
$hlblock = HL\HighloadBlockTabke::getById($hlbl)->fetch();
$entity = HL\HighloadBlockTable::compileEntity($hlblock);
$entity_data = $entity->getDataClass();

$dbData = $entity_data::getlist(array(
   'select' => array('*'),
   'order'  => array('UF_CATEGORIAY'=>'ASC'),
   'filter'  => array(
       '>=UF_DATE' => '2017-03-01 18:00:00',
       '<=UF_DATE' => '2017-01-01 18:00:00'
   )
));
while($data = $dbData->fetch())
{
   echo '<pre>';print_r($data);echo'</pre>';
}

custom field type:
UF_DATE - Date with time

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2017-03-02
@MrSen

First, the site format is used instead of Ymd H:i:s.
Second, use \Bitrix\Main\Type\DateTime

$dbData = $entity_data::getlist(array(
   'select' => array('*'),
   'order'  => array('UF_CATEGORIAY'=>'ASC'),
   'filter'  => array(
       '>=UF_DATE' => \Bitrix\Main\Type\DateTime::createFromUserTime('01.01.2017 18:00:00'),
       '<=UF_DATE' => \Bitrix\Main\Type\DateTime::createFromUserTime('03.01.2017 18:00:00')
   )
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question