tryConvertToDateTime() public static method

Try to convert string into DateTime object
public static tryConvertToDateTime ( mixed $value, array $formats = [] ) : DateTime
$value mixed
$formats array
return DateTime
 /**
  * Filter by date range
  * @param  Filter\FilterDateRange $filter
  * @return void
  */
 public function applyFilterDateRange(Filter\FilterDateRange $filter)
 {
     $conditions = $filter->getCondition();
     $value_from = $conditions[$filter->getColumn()]['from'];
     $value_to = $conditions[$filter->getColumn()]['to'];
     if ($value_from) {
         $date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
         $date_from->setTime(0, 0, 0);
         $this->data_source->where("CONVERT(varchar(10), {$filter->getColumn()}, 112) >= ?", $date_from->format('Ymd'));
     }
     if ($value_to) {
         $date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
         $date_to->setTime(23, 59, 59);
         $this->data_source->where("CONVERT(varchar(10), {$filter->getColumn()}, 112) <= ?", $date_to->format('Ymd'));
     }
 }
Ejemplo n.º 2
0
 /**
  * Format row item value as DateTime
  * @param  Row $row
  * @return string
  */
 public function getColumnValue(Row $row)
 {
     $value = parent::getColumnValue($row);
     if (!$value instanceof DateTime) {
         /**
          * Try to convert string to DateTime object
          */
         try {
             $date = DateTimeHelper::tryConvertToDateTime($value);
             return $date->format($this->format);
         } catch (DataGridDateTimeHelperException $e) {
             /**
              * Otherwise just return raw string
              */
             return $value;
         }
     }
     return $value->format($this->format);
 }
Ejemplo n.º 3
0
 /**
  * Filter by date
  * @param  Filter\FilterDate $filter
  * @return void
  */
 public function applyFilterDate(Filter\FilterDate $filter)
 {
     $conditions = $filter->getCondition();
     $date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]);
     $this->data_source->where('CONVERT(varchar(10), %n, 112) = ?', $filter->getColumn(), $date->format('Ymd'));
 }
Ejemplo n.º 4
0
 /**
  * Apply fitler date and tell whether row value matches or not
  * @param  mixed  $row
  * @param  Filter $filter
  * @return mixed
  */
 protected function applyFilterDate($row, FilterDate $filter)
 {
     $format = $filter->getPhpFormat();
     $condition = $filter->getCondition();
     foreach ($condition as $column => $value) {
         $row_value = $row[$column];
         $date = DateTimeHelper::tryConvertToDateTime($value, [$format]);
         if (!$row_value instanceof \DateTime) {
             /**
              * Try to convert string to DateTime object
              */
             try {
                 $row_value = DateTimeHelper::tryConvertToDateTime($row_value);
             } catch (DataGridDateTimeHelperException $e) {
                 /**
                  * Otherwise just return raw string
                  */
                 return FALSE;
             }
         }
         return $row_value->format($format) == $date->format($format);
     }
 }
 /**
  * Filter by date range
  * @param  Filter\FilterDateRange $filter
  * @return void
  */
 public function applyFilterDateRange(Filter\FilterDateRange $filter)
 {
     $values = $conditions[$filter->getColumn()];
     if ($value_from = $values['from']) {
         $date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
         $date_from->setTime(0, 0, 0);
         $expr = Criteria::expr()->gte($filter->getColumn(), $date_from->format('Y-m-d H:i:s'));
         $this->criteria->andWhere($expr);
     }
     if ($value_to = $values['to']) {
         $date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
         $date_to->setTime(23, 59, 59);
         $expr = Criteria::expr()->lte($filter->getColumn(), $date_to->format('Y-m-d H:i:s'));
         $this->criteria->andWhere($expr);
     }
 }
Ejemplo n.º 6
0
 /**
  * Filter by date range
  * @param  Filter\FilterDateRange $filter
  * @return void
  */
 public function applyFilterDateRange(Filter\FilterDateRange $filter)
 {
     $conditions = $filter->getCondition();
     $value_from = $conditions[$filter->getColumn()]['from'];
     $value_to = $conditions[$filter->getColumn()]['to'];
     $dataCondition = [];
     if ($value_from) {
         $date_from = DateTimeHelper::tryConvertToDateTime($value_from, [$filter->getPhpFormat()]);
         $dataCondition[$this->prepareColumn($filter->getColumn()) . '>='] = $date_from->setTime(0, 0, 0);
     }
     if ($value_to) {
         $date_to = DateTimeHelper::tryConvertToDateTime($value_to, [$filter->getPhpFormat()]);
         $dataCondition[$this->prepareColumn($filter->getColumn()) . '<='] = $date_to->setTime(23, 59, 59);
     }
     if (!empty($dataCondition)) {
         $this->data_source = $this->data_source->findBy($dataCondition);
     }
 }
Ejemplo n.º 7
0
 /**
  * Filter by date
  * @param Filter\FilterDate  $filter
  */
 public function applyFilterDate(Filter\FilterDate $filter)
 {
     $p1 = $this->getPlaceholder();
     $p2 = $this->getPlaceholder();
     foreach ($filter->getCondition() as $column => $value) {
         $date = DateTimeHelper::tryConvertToDateTime($value, [$filter->getPhpFormat()]);
         $c = $this->checkAliases($column);
         $this->data_source->andWhere("{$c} >= ?{$p1}")->andWhere("{$c} <= ?{$p2}")->setParameter($p1, $date->format('Y-m-d 00:00:00'))->setParameter($p2, $date->format('Y-m-d 23:59:59'));
     }
 }