我有以下使用 Symfony2 的查询,我想转换 i.fechaInicio
是 date
字段 Y-m-d
(2013-08-31 ) 到当前周数,以便我可以从当前周获取结果...
$now = new \DateTime();
$semana = $now->format('W'); // Week Number
$query = $this->getEntityManager()
->createQuery("
SELECT i
FROM PgeIncidenciasBundle:Incidencia i
WHERE i.fechaInicio = :semana // This is wrong because compares
// Y-m-d with W so no results found...
")
->setParameters(array(
'semana' => $semana
))
;
try {
return $query->getResult();
}
catch (\Doctrine\ORM\NoResultException $e)
{
return null;
}
最佳答案
使用BETWEEN运算符(operator):
$from = new DateTime;
$from->setIsoDate($from->format('o'), $from->format('W'));
$to = clone $from;
$to->modify('+6 day');
$query = $this->getEntityManager()
->createQuery("
SELECT i
FROM PgeIncidenciasBundle:Incidencia i
WHERE i.fechaInicio BETWEEN :from AND :to
")
->setParameters(array(
'from' => $from->format('Y-m-d'),
'to' => $to->format('Y-m-d'),
));
关于php - MySQL Where Date (not datetime) as Week of year,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18549182/