如何解析任何日期格式 [英] How to parse any date format

查看:115
本文介绍了如何解析任何日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的前端我使用jquery ui作为日期选择器和每种语言具有特定的日期格式。



Fr:d / m / Y
En:m / d / Y
...



确定,但现在在php部分,我必须使用相同的格式(Mysql Ymd)存储这些日期。我想避免使用开关盒和这样的东西,所以我开始寻找另一个选择。



我发现的最好的东西是



http://www.php.net/ manual / en / datetime.createfromformat.php 该功能将使我能够解析日期,如果我知道格式。



例如

  $ format ='Ym-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
/ *正确! * /

  php> $ i = new IntlDateFormatter(en_US.utf8,IntlDateFormatter :: SHORT,IntlDateFormatter :: NONE); 
php> echo $ i-> getPattern();
M / d / yy / * wtf * /

我错过了什么?



谢谢

解决方案

没有通用日期格式符号标准。符号y可以是2位数的年份,4位数年份,甚至是3个字母的缩写月份(不大可能),具体取决于来源。



IntlDateFormatter 的格式化符号记录在案 here 从文档可以看出,对于这个实现,d是没有前导零的日期,而dd包含前导零。 M和MM都代表前导零的月份。 yy是两位数的年份。



这个和jQuery UI的日期格式符号之间有足够的区别,你需要一个数据图来映射 IntlDateFormatter 格式符号到 jQuery UI datepicker格式符号,反之亦然。



这样的东西应该是足够的(未经测试):

  // PHP => jQuery 
$ formatMap = array(
'dd'=>'dd',
'd'=>'d',
'MM'=>'mm ',
'M'=>'mm',
'yy'=>'y'
//等等...
);

一旦你设置了地图,你可以创建你的 IntlDateFormatter 基于区域设置,将该格式的符号转换为jQuery UI日期格式符号,然后将格式发送到前端。



当用户发布返回所选日期,您可以反向执行相同操作,以返回到 IntlDateFormatter 符号。在这一点上,您可以使用 IntlDateFormatter :: setPattern()的组合将格式设置为MySQL风格,而 datefmt_format()(或等价物)实际格式化发布的日期。



希望这有帮助。


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

解决方案

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

// PHP => jQuery
$formatMap = array(
    'dd' => 'dd',
    'd'  => 'd',
    'MM' => 'mm',
    'M'  => 'mm',
    'yy' => 'y'
    // and so on...
);

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 of IntlDateFormatter::setPattern() to set the format to MySQL-style, and datefmt_format() (or equivalent) to actually format the posted date.

Hope this helps.

这篇关于如何解析任何日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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