如何解析任何日期格式
我正在尝试做一些聪明的事情来解析任何国际格式的日期。
在我的前端,我使用 jquery ui 作为日期选择器,每种语言都有其特定的日期格式。
Fr:日/月/年 En:月/日/年 ...
好吧,但现在在 php 部分我必须使用相同的格式(Mysql Ymd)存储这些日期。我想避免使用开关盒和类似的东西,所以我开始寻找另一种选择。
我发现的最好的事情是
http://www.php.net/manual /en/datetime.createfromformat.php 如果我知道格式,该函数将使我能够解析日期。
例如,
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');
好的,这是一个好的开始,但现在我想找到当前区域设置的标准日期格式。
所以我发现了这个:
http://www.php.net/manual/en/ intldateformatter.getpattern.php
但是我真的不知道如何让它工作,因为我得到的结果不一致:
php > $i = new IntlDateFormatter("fr_FR.utf8",IntlDateFormatter::SHORT,IntlDateFormatter::NONE);
php > echo $i->getPattern();
dd/MM/yy
/* correct ! */
但是
php > $i = new IntlDateFormatter("en_US.utf8",IntlDateFormatter::SHORT,IntlDateFormatter::NONE);
php > echo $i->getPattern();
M/d/yy /* wtf */
我错过了什么吗?
谢谢
I'm trying to make something clever in order to parse date in any international format.
On my frontend I use jquery ui as a date picker and each languages has its specific date format.
Fr: d/m/Y
En: m/d/Y
...
OK but now on the php part I have to store those date using the same format (Mysql Y-m-d). I would like to avoid using switch case and stuff like that so I started to look for another alternative.
The best thing I've found is
http://www.php.net/manual/en/datetime.createfromformat.php That function will enable me to parse the dates if I know the format.
For example
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2009-02-15');
Ok so that's a good start but now I'd like to find the standard date format for the current locale.
So I found this :
http://www.php.net/manual/en/intldateformatter.getpattern.php
But I dont really know how to get it to work because I get inconstitent results:
php > $i = new IntlDateFormatter("fr_FR.utf8",IntlDateFormatter::SHORT,IntlDateFormatter::NONE);
php > echo $i->getPattern();
dd/MM/yy
/* correct ! */
but
php > $i = new IntlDateFormatter("en_US.utf8",IntlDateFormatter::SHORT,IntlDateFormatter::NONE);
php > echo $i->getPattern();
M/d/yy /* wtf */
Am I missing something ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有“通用日期格式符号标准”。符号“y”可以是 2 位数的年份、4 位数的年份,甚至是 3 个字母的缩写月份(极不可能),具体取决于来源。
IntlDateFormatter
的格式符号记录在此处。正如您从文档中看到的,对于此实现,“d”是不带前导零的日期,而“dd”包含前导零。 “M”和“MM”都代表带有前导零的月份。 “yy”是两位数年份。这与 jQuery UI 的日期格式符号之间存在足够的差异,您需要一个数据映射来将
IntlDateFormatter
格式符号映射到 jQuery UI 日期选择器格式符号,反之亦然。像这样的东西应该足够了(未经测试):
设置好地图后,您可以根据区域设置创建
IntlDateFormatter
,将该格式的符号转换为 jQuery UI 日期格式符号,然后发送该格式到前端。当用户发回所选日期时,您可以反向执行相同操作以返回到
IntlDateFormatter
符号。此时,您可以使用IntlDateFormatter::setPattern()
的组合将格式设置为 MySQL 样式,并使用datefmt_format()
(或等效方法)来实际格式化发布日期。希望这有帮助。
There is no "universal date format symbol standard". The symbol "y" could be a 2-digit year, a 4-digit year, or even a 3-letter abbreviated month (highly unlikely), depending on the source.
The formatting symbols for
IntlDateFormatter
are documented here. As you can see from the documentation, for this implementation, "d" is the date without leading zeros, while "dd" contains the leading zeros. Both "M" and "MM" represent the month with leading zeros. The "yy" is the 2-digit year.There is enough difference between this and jQuery UI's date format symbols that you'll need a data map to map the
IntlDateFormatter
format symbols to jQuery UI datepicker format symbols, and vice versa.Something like this should be sufficient (untested):
Once you have your map set up, you can create your
IntlDateFormatter
based on locale, convert that format's symbols into jQuery UI date format symbols, and then send the format to the front-end.When the user posts back the chosen date, you can do the same in reverse to get back to
IntlDateFormatter
symbols. At that point, you can use a combination ofIntlDateFormatter::setPattern()
to set the format to MySQL-style, anddatefmt_format()
(or equivalent) to actually format the posted date.Hope this helps.