我通过堆栈日期时间试图组我的实体。每个实体都有以下各列:ID,设备,启动,停止,ip地址。(Symfony的3.1)PHP主义集团机构通过日期时间
我希望能够得到一个范围的日期,并将它们按日期(天)范围内的实体与每一天有多少小时有以下。
这是我迄今为止
public function findAllGroupedByDate($from = null, $to = null)
{
$from = isset($from) && !is_null($from) ? $from : date('Y-m-d', time());
$to = isset($to) && !is_null($to) ? $to : date('Y-m-d', time());
$from = new \DateTime("-3 days");
$to = new \DateTime("1 days");
$from = date("Y-m-d", $from->getTimestamp());
$to = date("Y-m-d", $to->getTimestamp());
$q = $this->getEntityManager()->createQueryBuilder()
->select("timelog")
->from("AppBundle:Timelog", "timelog")
->where("timelog.start BETWEEN :from AND :to")
->setParameter("from", $from)
->setParameter("to", $to)
->orderBy("timelog.stop")
->getQuery();
return $q->getResult();
}
public function findFromToday()
{
$q = $this->createQueryBuilder('t')
->select('t.id', 't.start', 't.stop', 't.stop')
->where("t.start >= :today")
->setParameter("today", date('Y-m-d', time()))
->groupBy("t.id", "t.stop")
->orderBy("t.start", "asc")
->getQuery();
return $q->getResult();
}
这是我的仓库类的代码。
,并从我的控制器看起来像这样的代码:
$timelogsRepo = $this->getDoctrine()->getRepository('AppBundle:Timelog');
// Grab logs from today
$timelogsCollection = $timelogsRepo->findFromToday();
$tmpLogs = $timelogsRepo->findAllGroupedByDate();
// THIS SECTION IS FOR CALCULATING HOURS & MINUTES
$minutes = 0;
$hours = 0;
foreach($timelogsCollection as $log)
{
$time1 = $log['start'];
$time2 = $log['stop'];
$interval = date_diff($time1, $time2);
$hours += $interval->h;
$minutes += $interval->i;
}
$minutes = $minutes >59 ? $minutes/60 : $minutes;
return $this->render(':Timelog:index.html.twig', [
'timelogs' => $logsOut,
'hours' => $hours,
'minutes' => $minutes
]);
到目前为止我能计算总花费的时间对于一个给定的一天(只有一天)。现在我想获得全部实体,由同日(日)他们组和间隔返回数据。
示例数据库表看起来像这样[ID,DEVICE_ID,启动,停止,ip地址]
1 1 2016-08-09 09:00:06 2016-08-09 12:00:06 127.0.0.1
2 1 2016-08-08 07:00:00 2016-08-08 13:00:00 127.0.0.1
3 1 2016-08-08 13:10:00 2016-08-08 15:05:00 127.0.0.1
因此,在这种情况下,我的输出会是这样的:
[
0 => ["date" => "2016-08-09", "hours" => 9.00, "ipaddress" => "127.0.0.1"],
1 => ["date" => "2016-08-09", "hours" => 1.45, "ipaddress" => "127.0.0.1"]
]
的时间间隔取决于启动和停止都是类型的DateTime
我试过使用doctrine-extensions:oro/doctrine-extensions但是现在我得到异常错误:
[2/2] QueryException: [Syntax Error] line 0, col 50: Error: Expected Unit is not valid for TIMESTAMPDIFF function. Supported units are: "MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR", got 'stop'
我的仓库方法是这样的:
public function findByToday()
{
$fields = array(
'DATE(stop) as stop',
'TIME(SUM(TIMESTAMPDIFF(stop, start))) AS tdiff',
'device_id',
'ipaddress'
);
$q = $this->createQueryBuilder('t')
->select($fields)
->where('DATE(stop) = :today')
->setParameter('today', date('Y-m-d', time()))
->groupBy('device_id')
->getQuery();
return $q->getResult();
}
我的数据库表:
id device_id start stop ipaddress
5 1 2016-08-09 09:00:06 2016-08-09 12:00:06 127.0.0.1
6 1 2016-08-08 07:00:00 2016-08-08 13:00:00 127.0.0.1
7 1 2016-08-08 13:10:00 2016-08-08 15:05:00 127.0.0.1
BTW我使用的Symfony 3,可能是这个问题?
托米斯拉夫感谢您的答复,我要去尝试一下,会让你知道如何去 –
嗨,我已经尝试过了,但仍然没有工作,现在我获取预期错误:[2/2] QueryException:[语法错误]第0行,第50列:错误:预期单位对于TIMESTAMPDIFF函数无效。支持的单位是:“MICROSECOND,SECOND,MINUTE,HOUR,DAY,WEEK,MONTH,QUARTER,YEAR”,得到'停止' –
在这里复制你的代码。 –