Which is better to format a date, PHP or in the MySQL query? would having MySQL format the date lower performance?
format date in PHP VS MySQL
It's a good question. I don't know the answer but it should be pretty easy to test.
I'll bet 50 cents that letting MySQL do it is going to be about .01% faster. Small enough that it doesn't matter.
Write a script that does this
- open a scratch file for writing
- $start = the current time
- select a million records from a database and formats the date
(can't be the same record a million times because the query result is cached) - write each to the scratch file (do not print to screen)
- $duration = the current time - $start
Run it a few times to see if the times are consistent
Then similarly, write a script that does this:
- open a scratch file for writing
- $start = the current time
- select a million records from a database but does not format the date
- format the date in PHP
- write each to the scratch file (do not print to screen)
- $duration = the current time - $start
While this might be too crude a test for some purposes, it will be good enough to determine if one is vastly slower than the other.... but I suspect that they're going to be almost equal.
I normally do it in the SQL query, because in PHP you would have to use multiple functions to first convert the date into a Unix timestamp and then format the timestamp into whatever you'd like.