CodeIgniter Forums
MVC options to consider for a simple date function. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: MVC options to consider for a simple date function. (/showthread.php?tid=53020)



MVC options to consider for a simple date function. - El Forum - 07-07-2012

[eluser]nullsys[/eluser]
Hi all!

I'm trying to wrap my head around MVC with what I want to achieve.
It's nothing complicated, but I'm considering the "best" way to achieve it.

I've written a very tiny function that checks a date and returns a new formatted string, based upon some conditions.

Now, I will want to apply this to almost all dates shown in all Views.
Assuming I place the function in a library (which may be built up later), so I employ DRY tactics, do I:

a. Load the library from the View, call the function and return directly back to the view.
b. Load the library in the controller, alter the date string in $data['Topics'] for EACH row, and just send that to the view like normal.
c. Load the library in the Model, and do the same as above.

Here's a lil background:

Code:
Model
---
Select * from Topics
return result_array() of all topics.

Controller
---
Send the $data['topics'] to the view.

View
---
Simply loop through each 'topics' and echo out the results.

It's nothing special, very basic.

I'm considering option B. But how would I have this function perform for each row in $data['Topics']?

Secret Option A...
Would I be best formating the date as I INPUT or UPDATE the table row in the first place, so I don't have to format whilst RETRIEVING?
This sounds good, but I don't like the idea of not using unix timestamps in MySQL

Secret Option B (my favourite)...
Should I just extend the Date library (since I'm loading it anyway), and create my own function there?
This sounds like the most reasonable, easiest, MVC, and DRY'ist.


MVC options to consider for a simple date function. - El Forum - 07-07-2012

[eluser]nullsys[/eluser]
I ended up extending the Date Helper and simply using my own function, called from the View.