The controller passes $results to the View.
The View has:
<?php foreach ($results as $result): ?>
<?php echo $result->time_start.' '.$result->time_finished;?>
<?php endforeach;?>
Also, I would like to display the difference ($result->time_finished minus $result->time_start) in format hh:mm:ss for each $result. If mm<10, then zero is shown, so 63 seconds are displayed as 00:01:03, not as 0:1:3.
Should I do all this staff in the view?
I believe it makes sense to use a function. Should I write this function in the model and call it from the view?
If not foreach, I would definitely call fhe function from the controller and pass the proper hh:mm:ss to the view from the controller.
But, what should I do in this case?
If you have a function that you need to use multiple times in your code, it makes sense to 1. create your own helper or 2. extend Kohana’s build in Date helper. Helpers are used for this kind of operations, not the Model and if the code gets to complex, also not the view.
I don’t know if you use Kohana 3.2 or 3.3, but there is a bit of a difference in naming convention between them (since 3.3 PSR-0 is implemented which means capitalized file and class names), but here are the DOCS for helpers:
http://kohanaframework.org/3.2/guide/kohana/helpers
http://kohanaframework.org/3.3/guide/kohana/helpers
and for extending existing classes:
http://kohanaframework.org/3.2/guide/kohana/files
http://kohanaframework.org/3.3/guide/kohana/files
I provide the answer if you use Kohana 3.2:
Solution 1: It would make sense to extend the Kohana Date class. Create a file named date.php in application/classes. The file should look like this:
In your view do this:
Date::some_function_to_convert($date1, $date2);Solution 2: Create your own helper in application/classes/helper dir. Name it date_conversion.php. Your file then looks like this:
In your view do this: