gpt4 book ai didi

php - MySQL Where Date (not datetime) as Week of year

转载 作者:行者123 更新时间:2023-11-29 00:26:46 27 4
gpt4 key购买 nike

我有以下使用 Symfony2 的查询,我想转换 i.fechaIniciodate 字段 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com