PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
The issue is that the date() function expects the second parameter to be in unixtime. Since your database is outputting in a string format, you need to convert to the unix date/time format which is where the strtotime() function comes in. It takes a string formatted date and converts it to unixtime.
VladSun wrote:I think it's preferable to do such things by using the DB engine and not in the PHP code. So, I find tchester's solution better than the others.
I do agree with this (somewhat). The reason I say somewhat is that in my framework I have user configurable options that allow the user to select different date formats to suit user preference. With this, I typically store my dates in unix time format which then makes the PHP conversion easier by eliminating the need for the strtotime() function.
It would take a bit of conversion to my framework, but I suppose I could still have user configurable options that change the SQL query to retrieve the date in the format I want instead of changing a PHP date format string, but would I really gain that much from it?
It is still a good idea for techster to know the issues of formatting a date in PHP.