#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171

    Is it a better practise to leave the "date functions" to mysql or php?


    Hello everyone;

    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.

    Thank you.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    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.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7171
    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.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Keep in in PHP unless absolutely necessary.

    Take a look at the PHP DateTime class.

IMN logo majestic logo threadwatch logo seochat tools logo