Date formatting: Code or Template? A theory/best practice question as opposed to a technical one. Should it be up to a module's code to format a timestamp into a date string, or the template that displays it?

E.g. the two ways of doing it (both with the same result):

MYMODULE.module

$variables['date'] = date('m-d-Y', $timestamp);
return theme_render_template('template.tpl.php', $variables);


TEMPLATE_FILE.tpl.php

&lt;span class="date"&gt;&lt;?php print $date; ?&gt;&lt;/span&gt;


Or:

MYMODULE.module

$variables['date'] = $timestamp;
return theme_render_template('template.tpl.php', $variables);


TEMPLATE_FILE.tpl.php

&lt;span class="date"&gt;&lt;?php print date('m-d-Y', $date); ?&gt;&lt;/span&gt;
Instead of date() you should use format_date() instead per Drupal.

https://api.drupal.org/api/drupal/includes%21common.inc/function/format_date/7.x

format_date takes various settings and user config into account when formatting the date, whereas just date is a php function, and thus, will not. You can also pass it a custom date format name.

While you can use format_date() at the template level, usually it is best to do it in a preprocess function and pass it along to the template. If you have a module that is supplying variables to a template, supply them upfront.

Variables should be print-ready for the user, with the option of overriding later (extending or preprocessing at the theme level, etc).