0

I'm looking for the right zf2 syntax to select timestamps / timeranges from the database. I know how to make where statements. However it seems that greaterThan() and lessThan() are not working with timestamps/datetime:

where = new Where();
$where->lessThan("datecolumn",$vardate);

I want to select all records older than 2 hours. so whats the right way to select date with zend framework 2?

Thx, I really appreciate your help

Manuel
  • 9,112
  • 13
  • 70
  • 110

2 Answers2

3

This works fine (just a sample) -

 $select = new Select('album');

 $created = date('Y-m-d H:i:s', strtotime("-2 hours"));

 $where = new Where();
 $where->lessThanOrEqualTo('created', $created);
 $select->where($where);

 $resultSet = $this->tableGateway->selectWith($select);
Kunal Dethe
  • 1,254
  • 1
  • 18
  • 38
0

Try something like this in your mapper method:

$selectRecords = $this->tableGateway->getSql()->select();
$selectRecords->columns(array('id'))
              ->where->greaterThanOrEqualTo('dateColumn', $startDate)
                     ->lessThanOrEqualTo('dateColumn', $endDate)

; $resultSet = $this->tableGateway->selectWith($selectRecords);

Chukky Nze
  • 720
  • 3
  • 13
  • It does work with dates, datetime, timestamp as expected. Please check the date-format of the `dateColumn`, `$startDate` and `$endDate`. You need the records having date values of less than 2 hours, so your column must be a datetime? If it is, then the format has to be like '2014-05-06 10:20:30' – Kunal Dethe Jun 12 '14 at 06:14
  • Sorry for that, I meant the `datecolumn` and the `$vardate`. – Kunal Dethe Jun 12 '14 at 06:25