从其他语言的名称获取月份数
如果我有某种语言的月份名称(比如说阿尔巴尼亚语),我如何获得与其对应的数字(PHP)?除了使用该语言的月份名称创建数组的明显方法(恐怕这很容易出现拼写错误)之外,我认为您可以使用 setlocale 但我不知道下一步该怎么做。
If I have a month name in a certain language(let's say Albanian), how do I get the number corresponding to it (in PHP)? Besides the obvious method of making an array with the month names of that language (I'm afraid that's quite vulnerable to spelling mistakes) I presume that you can use setlocale but I can't figure out what to do next.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能是错的,但据我所知,您可以使用区域设置来生成月份名称,但没有能够解析非英语名称的内置日期函数。
生成要比较的名称列表应该相当容易:
但是,某些语言中可能存在其他拼写,因此此方法的可靠性实际上取决于您的确切需求。
PHP 有一些国际化函数,这可能是更好的选择。 IntlDateFormatter::parse() 方法似乎完全符合您的要求问。但是,其要求包括
PHP 5 >= 5.3.0、PECL intl >= 1.0.0
。I may be wrong but, as far as I know, you can use locales to generate month names but there aren't builtin date functions that are able to parse non-English names.
It should be fairly easy to generate a list of names to compare against:
However, it's possible that there're alternative spellings in some languages so the reliability of this method actually depends on your exact needs.
PHP has some Internationalization Functions that might be a better bet. The IntlDateFormatter::parse() method appears to do exactly what you are asking. However, its requirements include
PHP 5 >= 5.3.0, PECL intl >= 1.0.0
.看一下 i18n 包。它提供了 php 中国际化的类和函数。
http://php-flp.sourceforge.net/getting_started_english.htm#dates
Take a look at the i18n package. It provides classes and functions for internationalization in php.
http://php-flp.sourceforge.net/getting_started_english.htm#dates