I have two fields with datetime formats in Symfony 2.Now I want to get total hours after the form is submitted
entity.php
private $timein;
private $timeout;
public function setTimein($timein)
{
......
public function getWorkHour()
{
$hour1 = 0; $hour2 = 0;
$date1 = $this->timein;
$date2 = $this->timeout;
$datetimeObj1 = new \DateTime($date1);
$datetimeObj2 = new \DateTime($date2);
$interval = $datetimeObj1->diff($datetimeObj2);
if($interval->format('%a') > 0){
$hour1 = $interval->format('%a')*24;
}
if($interval->format('%h') > 0){
$hour2 = $interval->format('%h');
}
$hour = ($hour1 + $hour2) . " hours.";
return $hour;
}
show.html
<th>No of Hour</th>
<td>{{ entity.getWorkHour() }}</td>
This will throw an error
An exception has been thrown during the rendering of a template ("DateTime::__construct() expects parameter 1 to be string, object given") in ...
How would you simply get number of hours between two hours in same date?
$interval = $this->timeout->diff($this->timein)
– Matteo