I have a database that has a few unknown months or days formatted like so: 1999-01-00 / 1999-00-00.
Now, I’ve been using DateTime() to format the days that are full, which works fine.
I’d like to have abbreviations for unknown days, but known months using $DateTime->format('M Y'); which outputs Sep 1999
and
$DateTime->format('Y'); for 1999 that is for unknown months and days.
DateTime() Doesn’t allow 00 values for months/days, so what are some ways to get around this?
Unfortunately, the DateTime class is pretty much a wrapper for a UNIX timestamp. (I said pretty much, I know there’s more to it than just that.) The way that it works with dates were one of the elements is zero is simply to calculate it as a synonym for the previous period. (e.g.: April 0 is calculated to become March 31.)
Really, you just have to bypass PHP’s built in date functionality and write your own. The good news is that you can indeed do this. MySQL will actually return values as simple strings, and, when it comes to dates, the natural response that the actual C libraries use is to return the date pretty much as an ISO 8601 string. (There’s that phrase pretty much again.)
If I were writing this, I would read the value raw with
mysql_fetch_row(or your own method) and check if all the elements (i.e.: year, month, day) are defined. If they are, use the DateTime method. If not, I would write a piece of code to pull apart the elements that are defined and format it accordingly.