Suppose that you want to show a user list, on a web application having to handle custom date formats (depending on current selected interface language).
Several locations for applying the date format seem possible. Which one would you use, and why?
At model, for example doing a
DATE_FORMAT(user.birthdate, '%d/%m/%Y')
for date fields (supposing we have a MySQL DB).At controller level, for example (supposing we use PHP):
$users = UserModel::getUsers(); // loops through results before display to format dates. // in the $users structure, we have to know which fields are dates or not $users = MagicFunctionFormattingDates($users); $view = new View('someTemplate'); $view->assign('users', $users);
At view level, for example (supposing we use a template language such as Smarty):
{foreach key=keyitem item=row from=$users} <tr> {foreach key=keyfield item=field from=$row.fields} {* Here $field should be an Object or associative array holding its 'type' (is it a date or something else) *} <td>{MagicFunctionForFormatting field=$field}</td> {/foreach} </tr> {/endforeach}
A similar and related question goes for sumbitting dates (e.g. on a user registration form). Where should the date conversion take place:
In the controller processing and validating the form, before calling the model?
In the model?