Which is the best way to display a date in a more human-readable way?
1st. using MySQL DATE_FORMAT() function, like this:
SELECT DATE_FORMAT( `date`, '%d %b %Y' ) AS `date` ...
or 2nd. with PHP, like this:
<?php echo date('j M Y, g:i a', strtotime($post->date)); ?>
Which one would you recommend , regarding the performance or the best-practices, when fetching and displaying many records (100 + ) ?
Hi Lykos,
Personally, I’d go for the second option, as formatting data for display is the responsibility of the view. This makes sense when you consider that you might want to have different views of the same data, with different date formats.
In terms of performance, although using the built-in MySQL functions are most likely faster, I don’t think it’s going to make a great difference in practice. Even when dealing with reports with a lot of records, you’re likely to paginating them anyway.
If you are extracting the date for viewing and will not need it for any other processing then the database can do the conversion. If the date needs processing in any way whatsoever after extracting it then leave formatting it until you are ready to display it.