我通過堆棧日期時間試圖組我的實體。每個實體都有以下各列: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」,得到'停止' –
在這裏複製你的代碼。 –