title: Formatting dates and times in a controller
body: <p>I have a situation where I need to format date and time information in a controller. There are many solutions that are close, but none quite work. I've had <a href="https://github.com/dereuromark/cakephp-ajax/issues/9" rel="nofollow">an extended conversation about it elsewhere</a>, but will summarize the salient points here.</p>
  
  <p>The thing that I'm trying to accomplish is to create a flash message with formatted dates and times in it, which can be returned either through Ajax or as a "standard" flash message if the user happens to not have JavaScript available.</p>
  
  <p>I am using dereuromark's <a href="https://github.com/dereuromark/cakephp-ajax/" rel="nofollow">Ajax plugin</a> for handling Ajax responses, and FriendsOfCake's <a href="https://github.com/FriendsOfCake/bootstrap-ui" rel="nofollow">Bootstrap UI plugin</a> for Flash messages.</p>
  
  <p>I tried using a custom flash element, but this isn't rendered until the view is displayed, meaning that the formatted result isn't available when the Ajax plugin creates its response; it instead gets the array describing the flash output that would be generated in the non-Ajax case.</p>
  
  <p>I tried tweaking the Ajax and Bootstrap UI plugins so that the flash message would be rendered at a useful time, but then it has all of the HTML wrapping it, which I don't need or want in the Ajax case.</p>
  
  <p>It seems that the solution is to format the message in the controller before passing it to the flash function, but formatting of dates and times is done in a helper, which I don't normally have access to in the controller. Note that I have a custom Time helper which takes into account the current user's preferred time and date format (e.g. 6:00PM vs 18:00 and Nov 16, 2015 vs 2015-11-16).</p>
  
  <p>Three options that I've considered from here are:</p>
  
  <ol>
  <li><p>Make some globally-available date and time formatting functions that use the user's preferences and call those functions from my helper. I don't like this because it feels like it moves implementation away from where it should be, and because those global functions would have to do their own formatting instead of passing it on to the standard Time helper as mine currently does. My implementation is therefore possibly going to break if there is a change in the underlying implementation of dates and times, e.g. from Carbon to something else.</p></li>
  <li><p>Create an instance of the TimeHelper in the controller and call it to do the formatting for me. I don't like this because it feels not MVC, and the way that the helper is created would likely be at least a little dependent on implementation details within CakePHP, again making it likely to break in a future version.</p></li>
  <li><p>Re-do all of the changes I made to the two plugins, but specify a different flash element (which skips all the HTML and just formats the message itself) when the action is called via Ajax. I don't like this because it relies on two different groups accepting my updates (I'm not willing to use an edited version of their releases, as this causes upgrade nightmares), and it's not very DRY (my message formatting code is duplicated across two elements).</p></li>
  </ol>
  
  <p>Is there some magical fourth option that I haven't considered? Some slight alteration to one of these that would make it workable? Is this a use case that standard CakePHP should provide support for? If the answer to all of these is no, then which of these hacks is the least offensive?</p>
  
  <p>If anything here is unclear, let me know and I'll attempt to elaborate. I don't see where adding code to this would be helpful, but I'm pretty close to the problem. :-)</p>