我正在使用symfony3,我有一个名为Presence的实体来注册每个员工的入职时间和出勤时间。 注册工作很好,但我想计算特定月份的工作时间总数。
事实上,我使用这种方法计算白天的工作总时数:
public function hoursByDay()
{
$interval1 = $this->entryTimeFirstHalf->diff($this->entryTimeFirstHalfr);
$interval2 = $this->entryTimeSecondHalf->diff($this->outTimeSecondHalf);
$e = new \DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
return $f->diff($e)->format("%H:%I");
}
但我无法按月计算的工作时数总计。
/**
* Presence
*
* @ORM\Table(name="presence")
* @ORM\Entity(repositoryClass="AppBundle\Repository\PresenceRepository")
*/
class Presence
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="date_presence", type="date")
*/
private $date;
/**
* @var \DateTime
*
* @ORM\Column(name="entry_time_first_half", type="time")
*/
private $entryTimeFirstHalf;
/**
* @var \DateTime
*
* @ORM\Column(name="out_time_first_half", type="time")
*/
private $outTimeFirstHalf;
/**
* @var \DateTime
*
* @ORM\Column(name="entry_time_second_half", type="time")
*/
private $entryTimeSecondHalf;
/**
* @var \DateTime
*
* @ORM\Column(name="out_time_second_half", type="time")
*/
private $outTimeSecondHalf;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $employee;
答案 0 :(得分:0)
试试这个SQL:
SELECT `employee_id`, SUM( TIMESTAMPDIFF( HOUR, `entry_time_first_half`, `out_time_second_half` ))
FROM `presence`
WHERE MONTH(`date`) = 7 AND YEAR(`date`) = 2018
GROUP BY `employee`