CodeIgniter Forums
Advice on handling date preferences - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Advice on handling date preferences (/thread-53304.html)



Advice on handling date preferences - El Forum - 07-18-2012

[eluser]gregormck[/eluser]
Hi there,

I'm building an app at the moment where I want to give users the option to choose their preferred date format. For example:

US users might want 07/28/2012

Where UK users might want 28/07/2012

My preference would be to handle dates regardless of preference in the same format in the controller and in the model - meaning that the controller will push a date to a view in the format of "28/07/2012", but then parse it to "07/28/2012" for a US user (as an example).

When it comes to inputting a date via a date picker for example the user will see "07/28/2012", but the form will post "28/07/2012" meaning I don't need to do any extra server side checks i.e. check date format preference.

Is this the "right" approach to take, or should I look to implement some server side date format preference checks (outside filtering, validation, etc.)?

The app will use dates a lot so I want to implement the right system design - just not sure about the best way to go about handling preferences. Any advice would be much appreciated.

Thanks,

Gregor




Advice on handling date preferences - El Forum - 07-18-2012

[eluser]Aken[/eluser]
Your best option is to always store and handle dates as unix timestamps, and then format them for display in the front end.


Advice on handling date preferences - El Forum - 07-18-2012

[eluser]CroNiX[/eluser]
I personally prefer datetime over timestamps as it's a lot easier to use mysqls powerful built-in time/date functions without converting.

http://stackoverflow.com/questions/409286/datetime-vs-timestamp

You can easily format the desired date output as part of the query or use strtotime() during output.

Timestamps also have an upper limitation to the year 2038, which isn't that far off.


Advice on handling date preferences - El Forum - 07-18-2012

[eluser]Aken[/eluser]
DATETIME does come with built-in MySQL functionality, yes. They both have their pros and cons. DATETIME is not UTC-specific, so you'll need to keep that in mind if you plan to change dates/times into a user's time zone.

That SO question has good info, give it a read.


Advice on handling date preferences - El Forum - 07-24-2012

[eluser]gregormck[/eluser]
Thanks both for the reply. I guess in a way what I want is for dates to be posted in the same format every time regardless of date preference set by the user. This is so that I don't need to do anything in the controller or model to get the date into the format I need.

Will read the SO article - thanks