DateTime :: createFromFormat在PHP< 5.3.0 [英] DateTime::createFromFormat in PHP < 5.3.0
问题描述
我正在寻找与 DateTime :: createFromFormat 但是我需要它在一个运行v5.3以前的PHP版本的环境中工作。
基本上,我需要提供一种格式,就像您使用日期()函数,然后我需要根据该格式解析/验证一个字符串,如果字符串的格式正确和有效的日期,则返回一个时间戳。
<任何人都知道我可以在哪里找到这样的东西,还是自己写?
再次,这必须使用特定的格式,作为论据。格式可以是任何东西,所以不能保证我只能使用 strtotime()。
DateTime :: createFromFormat
和 date_parse_from_format
已添加到PHP 5.3中,因为对该功能的需求很高,特别是来自不使用美国日期/时间格式的用户的开发人员。
在这些之前,您必须开发一个特定的功能解析您使用的格式;使用PHP< 5.3,通常做的是:
- 确定应用程序将接受哪种格式
- 显示一些消息,说明您的输入应为JJ / MM / AAAA (DD / MM / YYYY的法语)
- 检查输入是否正确,关于该格式
- 并解析它将其转换为PHP可以理解的日期/时间。
这意味着应用程序和开发人员通常不允许使用多种格式,因为每种格式都意味着一种不同的加法验证+解析功能。
如果你真的需要这种功能,那么允许任何可能的格式,我恐怕你必须自己写: - (
也许看看 date_parse_from_format
的来源可以帮助,如果你明白C代码,应该是像$ code> ext / date / php_date.c - 但似乎并不简单:它调用 timelib_parse_from_format
函数,它在 ext / data / lib / parse_date.c
中定义,看起来很友善^^
I'm looking for a function identical to DateTime::createFromFormat but I need it to work in an environment running a version of PHP which is older than v5.3. Basically I need to provide a format, like you'd use with the Date() function, and I then need to parse/validate a string against that format, and return a timestamp if the string is formatted correctly and a valid date.
Anyone know where I can find something like that, or do I have to write it myself?
Again, this has to work with a specific format, provided as an argument. The format could be anything, so there's no guarantee I can just use strtotime().
DateTime::createFromFormat
and date_parse_from_format
have been added in PHP 5.3 because there was a high demand for that feature, especially from developpers who code for users who don't use US date/time formats.
Before those, you had to develop a specific function to parse the format you were using ; with PHP < 5.3, what is generally done is :
- Decide which format will be accepted by the application
- Display some message saying something like "your input should be JJ/MM/AAAA" (French for DD/MM/YYYY)
- Check that the input is OK, regarding to that format
- And parse it to convert it to a date/time that PHP can understand.
Which means applications and developpers generally didn't allow for that many formats, as each format meant one different additionnal validation+parsing function.
If you really need that kind of function, that allows for any possible format, I'm afraid you'll kind of have to write it yourself :-(
Maybe taking a look at the sources of date_parse_from_format
could help, if you understand C code ? It should be in something like ext/date/php_date.c
-- but doesn't seem to be that simple : it's calling the timelib_parse_from_format
function, which is defined in ext/data/lib/parse_date.c
, and doesn't look that friendly ^^
这篇关于DateTime :: createFromFormat在PHP< 5.3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!