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!
I am trying to pull a date out a MySQL database with the following format: yyyy-mm-dd, a non-timestamp date. I want to pull things out like the day of the week and other date type niceties. Here is some code, but I really probably just need a function name to work with:
A non-timestamp date so you mean the date field is of DATETIME? Or are you saying the YYYY-MM-DD is being stored as a field of characters?
Show us your query. Either you will have to do a "UNIX_TIMESTAMP(startdate) AS startdate" in the query, or format it as a string right in mySQL using DATE_FORMAT().
I wanted to handle it back in the query, but my hosting company is running MySQL 4.0.x. Time for a new hosting company? It is a DATETIME data type though.
What exactly is the issue? MySQL has some pretty nifty date and time functions to handle dates. What is currently in the database and what do you want it to look like?
Cool. strtotime() did the trick. The content is not dynamic so maybe I won't get any errors? I don't think I am clever enough to make explode work. So you explode() and then mktime() something like that?
mjmacarty wrote:I wanted to handle it back in the query, but my hosting company is running MySQL 4.0.x. Time for a new hosting company? It is a DATETIME data type though.
If it is DATETIME then definitely use mysql's DATE_FORMAT() function (look it up)... it will be faster than strtotime().
I think the biggest issue is the hosting company I am using is running the las t version of MySQL before all the coolest functionality was added. Hence I have to find innovative ways to write extra code.
Most everything good in MySQL comes after version 4.0
More than one way but strtotime() is the slowest and sloppiest... either use:
* DATE_FORMAT to have mysql format the string for you or:
* UNIX_TIMESTAMP to output the date as a unix timestamp and then hand it over to date() in PHP
Taking something that's already in a numeric date format, outputting it as a string then walking through the string to get it back to the data type it was originally is just silly.