I created a system that stores a non-standard date into the database. For every post a user creates, a date is stored with this format Month day, year, i.e. September 15, 2012. I don’t think MySQL has a way to organize this data so I have to do so manually.
I could not come up with a way of doing this.
Please help me out,
thanks.
Sorting what you have will be very difficult. Month is first, yet it’s an alpha and can’t be sorted that way. Day is second, and year is last.
You’ll be much better off using MySQL’s
DATETIMEorTIMESTAMPfield types. At the very least, you can store it a unix timestamp withINT(10)and sort numerically.I’ve been using the unix timestamp approach for years, but I’m slowly switching over to
TIMESTAMPbecause you can do a lot more with MySQL’s native date formats. As an added bonus, you can actually look at and understand the dates in phpMyAdmin without having to constantly write FROM_UNIXTIME(field). You just have to tell MySQL what timezone you’re operating on when you make the connection (if usingTIMESTAMP).You should be able to easily convert what you have using strtotime. It will return
FALSEif it can’t figure it out. How smoothly this goes depends on if you did any sanitization of the dates. If not, you may be able to do some cleanup with preg_replace.After you’ve converted the data in the database, you can display it how you want with PHP’s date function or MySQL’s DATE_FORMAT.
What ever you choose to do, avoid sorting it in PHP. Let your database do as much of the sorting and fetching as possible.