从数据库使用DateTime()格式化部分未知日期? [英] Formatting partial unknown dates with DateTime() from a database?

查看:94
本文介绍了从数据库使用DateTime()格式化部分未知日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其中的某些未知月份或日期的格式如下:1999-01-00/1999-00-00.

I have a database that has a few unknown months or days formatted like so: 1999-01-00 / 1999-00-00.

现在,我一直在使用DateTime()格式化已满的日子,效果很好.

Now, I've been using DateTime() to format the days that are full, which works fine.

我想为未知的日子使用缩写,但使用$DateTime->format('M Y');的已知月份使用缩写,输出Sep 1999

I'd like to have abbreviations for unknown days, but known months using $DateTime->format('M Y'); which outputs Sep 1999

$DateTime->format('Y');表示1999,表示未知的月份和日期.

$DateTime->format('Y'); for 1999 that is for unknown months and days.

DateTime()不允许数月/天的00值,那么有什么方法可以解决此问题?

DateTime() Doesn't allow 00 values for months/days, so what are some ways to get around this?

推荐答案

不幸的是,DateTime类几乎是UNIX时间戳的包装器. (我说了很多,我知道不仅限于此.)它与日期(其中一个元素为零)一起使用的方式只是将其计算为上一个时期的同义词. (例如:4月0日被计算为3月31日.)

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.)

真的,您只需要绕过PHP的内置日期功能并编写自己的功能即可.好消息是您确实可以做到这一点. MySQL实际上将以简单的字符串形式返回值,并且在涉及日期时,实际的C库使用的自然响应是将日期返回的方式几乎与ISO 8601字符串一样. (再次有很多这个短语.)

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.)

如果我正在编写此代码,则将使用mysql_fetch_row(或您自己的方法)读取raw值,并检查是否定义了所有元素(即:年,月,日).如果是这样,请使用DateTime方法.如果没有,我将编写一段代码来分解定义的元素并相应地对其进行格式化.

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.

这篇关于从数据库使用DateTime()格式化部分未知日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆