April 9th, 2013, 01:22 AM
Is it a better practise to leave the "date functions" to mysql or php?
This is very general question I dont need a "depends" answer. I am not asking for specific situation either.
Generally speaking, if you were to make an application like Devshed or Facebook, would you leave the "date functions" to mysql or php? Please explain why either way.
April 9th, 2013, 03:35 AM
It is not easy to give a specific answer to a generally question. most of the solution will depends at which task/need you have.
Generally for the sites you have as examples, I would use MySql to get the date and/or time in a ISO format (standard format?) and then use the PHP functions to format the view/output of the date, which could be based on the user localization setting.
Why i would do this? because the database/SQL is mostly about storing and getting data, it does not need to know anything about how the user want it displayed.
April 9th, 2013, 05:59 PM
I try to keep date handling in PHP as much as I can, however there are some queries where it's necessary to do the date handling in query.
Date handling between PHP and MySQL is not actually completely interchangeable. For example, one of them starts the week on Monday and the other starts the week on Sunday, and last time I checked there was no way to change either behavior. Also you will likely run into problems if the two systems are not set to the same timezone. For simplicity, I always force both to UTC for everything except final display.
April 9th, 2013, 06:12 PM
Keep in in PHP unless absolutely necessary.
Take a look at the PHP DateTime class.