Yii2 datetime difference calculation between two dates and inserting the difference in db
<p><a href="https://i.stack.imgur.com/usZPl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/usZPl.png" alt="enter image description here"></a></p>

<p>There are 3 fields in task table-> expected_start_datetime,expected_end_datetime,time_allocated
While creating a task expected start and end datetime is selected and saved in the records.</p>

<p>What I am trying to do is to find the difference between the two dates in hours and minutes and save the value inside the "time_allocated" while creating the task and later on the update or view page use/display the time allocated value from the records.</p>

<p>Trying something like this in the task controller action create</p>

<pre><code>$diff = ((strtotime($model-&gt;expected_start_datetime) - strtotime($model-&gt;expected_end_datetime)) / (60 * 60 * 24));

        $model-&gt;time_allocated = $model-&gt;time_allocated + $diff;
</code></pre>

<p><a href="https://i.stack.imgur.com/MB3yH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MB3yH.png" alt="enter image description here"></a></p>