将时间戳列中的值截断为仅日期部分,删除条令1.2中的时间


Truncate the value in timestamp column to only date portion,removing the time in Doctrine 1.2

我想比较两个日期,如果一对多关系中的列中已经存在相同的日期,则用户输入的日期不应保存在Mysql中。我的问题是,日期列是时间戳类型,因此两个相同的日期有不同的时间,它仍然会保存在数据库中,因为它们仍然是"不同的值",比如"2004-13-12 8:00:00"与"2014-13-12 8:07:08"不同。我可以在Mysql 中完成

SELECT date_created 
FROM koleks
WHERE 
-- Truncate the datetime to a date only
DATE(date_created) = '2014-03-12'
AND id = 3942

我尝试过学说,但没有成功。

public function getDateCreate($date,$id) {
    //$date =date('Y-m-d H:i:s',strtotime('2014-03-12 8:00:00'));this will 
   // $id = 3942;//work
    $q = $this->createQuery('k')
                ->select('k.date_created')
                ->addSelect('k.loan_id')
                ->from('Koleks k')
                ->innerJoin('k.Loans l')
                ->andWhere('k.date_created=?',$date)
                ->andWhere('k.loan_id=?',$id);
               return $q->execute();

//动作

  $dateOfPayment = '2014-01-10';
  $ids = 1234;
  $duplicates = Doctrine_Core::getTable('Koleks')->getDateCreate($dateOfPayment,$id);
                        if (count($duplicates) > 0) {
                           $this->fieldErrors['date_of_payment_'.$loan['id']] = 'Error Duplicate date.';
                        }
                       else {
                             //code to save in database;
                        }

有什么想法吗?如果两个日期值的日期部分具有相同的值,而不管其时间(小时、分钟、秒),我想在操作中抛出一个错误。

条令将愉快地接受dql:中的DATE()函数

->andWhere('DATE(k.date_created) = ?', $date)

在MySQL中,您可以使用DATE()函数来删除DATETIME的时间部分。