php 使用 Doctrine 比较日期时间之间的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29918007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare dates between datetimes with Doctrine
提问by user3396420
I have a Syfmony2 app with a table which has a date field. This date field is a DateTime type.
我有一个带有日期字段的表的 Syfmony2 应用程序。此日期字段是 DateTime 类型。
I need to get all the entities which the same date as now.
我需要获取与现在相同日期的所有实体。
But if I do:
但如果我这样做:
$now = new \DateTime();
$data = $entityRepository->findByDate($now);
I get 0 results because Doctrine is comparing the DateTime object, and I need to compare only the year, month and day, not hours... only de Date object, not the DateTime.
我得到 0 个结果,因为 Doctrine 正在比较 DateTime 对象,我只需要比较年、月和日,而不是小时……只有 de Date 对象,而不是 DateTime。
Any Idea? Thanks :D
任何的想法?感谢:D
回答by goto
I see this simple way:
我看到这个简单的方法:
$now = new \DateTime();
$data = $entityRepository->getByDate($now);
then in your repository
然后在您的存储库中
public function getByDate(\Datetime $date)
{
$from = new \DateTime($date->format("Y-m-d")." 00:00:00");
$to = new \DateTime($date->format("Y-m-d")." 23:59:59");
$qb = $this->createQueryBuilder("e");
$qb
->andWhere('e.date BETWEEN :from AND :to')
->setParameter('from', $from )
->setParameter('to', $to)
;
$result = $qb->getQuery()->getResult();
return $result;
}
回答by Дмитрий Будницкий
Method in repository
存储库中的方法
public function getDays(\DateTime $firstDateTime, \DateTime $lastDateTime)
{
$qb = $this->getEntityManager()->createQueryBuilder()
->select('c')
->from('ProjectBundle:Calendar', 'c')
->where('c.date BETWEEN :firstDate AND :lastDate')
->setParameter('firstDate', $firstDateTime)
->setParameter('lastDate', $lastDateTime)
;
$result = $qb->getQuery()->getResult();
return $result;
}
And action
和行动
public function calendarAction()
{
$currentMonthDateTime = new \DateTime();
$firstDateTime = $currentMonthDateTime->modify('first day of this month');
$currentMonthDateTime = new \DateTime();
$lastDateTime = $currentMonthDateTime->modify('last day of this month');
$days = $this->getDoctrine()
->getRepository('ProjectBundle:Calendar')
->getDays($firstDateTime, $lastDateTime);
return ['days' => $days];
}
回答by Nicolai Fr?hlich
There is a difference between the date
and datetime
types in doctrine.
date
和datetime
类型在教义上是有区别的。
date: Type that maps a SQL DATETIME to a PHP DateTime object.
datetime: Type that maps a SQL DATETIME/TIMESTAMP to a PHP DateTime object.
date:将 SQL DATETIME 映射到 PHP DateTime 对象的类型。
datetime:将 SQL DATETIME/TIMESTAMP 映射到 PHP DateTime 对象的类型。
Make sure you have set the column type to date
instead of datetime
.
确保您已将列类型设置为date
而不是datetime
。
Alternatively - as a workaround - you could get the day from the original date1 and then search between a same-day date2 -> 00:00:00 and same-day date3 -> 23:59:59 using a custom repository method.
或者 - 作为一种解决方法 - 您可以从原始 date1 中获取日期,然后使用自定义存储库方法在同一天 date2 -> 00:00:00 和同一天 date3 -> 23:59:59 之间进行搜索。