【问题标题】:PHP: Parse date from localized formatPHP:从本地化格式解析日期
【发布时间】:2015-07-23 09:38:35
【问题描述】:

我需要在多语言应用程序中解析日期字符串。每个用户都有自己的语言环境和日期格式。

使用方法:

new DateTime($datestr);

date_parse($datestr);

使用本地化日期格式?

假设 mm/dd/yyyy 为 EN 日期格式,dd/mm/yyyy 为 IT 日期格式,我做了这个测试:

<?php
echo "EN locale<br>\r\n";
setlocale(LC_ALL, 'en_US');
$date="01/02/2015"; //2th Jan
$date_array=date_parse($date);
$mktime=mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']);
$datetime=new DateTime($date);    
echo date("Y-m-d",$mktime);
echo "<br>\r\n";
echo $datetime->format('Y-m-d');
echo "<br>\r\n";

echo "IT locale<br>\r\n";
setlocale(LC_ALL, 'it_IT');
$date="01/02/2015"; //1th Feb
$date_array=date_parse($date);
$mktime=mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']);
$datetime=new DateTime($date);    
echo date("Y-m-d",$mktime);
echo "<br>\r\n";
echo $datetime->format('Y-m-d');

结果是相同的输出,两种语言环境设置都以 mm/dd/yyyy 格式提供解析。输出始终为 2015-01-02(2 月 2 日)

【问题讨论】:

  • 看到这个关于你需要什么的类似问题stackoverflow.com/questions/8827514/…。您需要使用 set_localegetDateFormat
  • 为什么我找不到任何关于 getDateFormat() 的文档?
  • 抱歉@Tobia,我急于写我的评论,getDateFormat,是一个自定义函数,将根据语言环境返回日期格式

标签: php parsing date localization


【解决方案1】:

这就是答案:

$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);

这是与我的答案一起使用的上一个测试。

<?php
echo "EN locale<br>\r\n";
$date="01/02/2015"; //2th Jan
$formatter = new IntlDateFormatter("en_US", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);
$datetime=new DateTime();
$datetime->setTimestamp($unixtime);
echo $datetime->format('Y-m-d');
echo "<br>\r\n";

echo "IT locale<br>\r\n";
$date="01/02/2015"; //1th Feb
$formatter = new IntlDateFormatter("it_IT", IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
$unixtime=$formatter->parse($date);
$datetime=new DateTime();
$datetime->setTimestamp($unixtime);
echo $datetime->format('Y-m-d');
echo "<br>\r\n";

不幸的是,我无法获得赏金...... :-)

【讨论】:

    【解决方案2】:
    <?php
    echo "EN locale<br>\r\n";
    setlocale(LC_ALL, 'en_US');
    $date="01/02/2015"; //2th Jan
    $date_array=date_parse($date);
    $mktime=mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']);
    $datetime=new DateTime($date);    
    echo date("m/d/Y",$mktime);
    echo "<br>\r\n";
    echo $datetime->format('m/d/Y');
    echo "<br>\r\n";
    
    echo "IT locale<br>\r\n";
    setlocale(LC_ALL, 'it_IT');
    $date="01/02/2015"; //1th Feb
    $date_array=date_parse($date);
    $mktime=mktime($date_array['hour'], $date_array['minute'], $date_array['second'], $date_array['month'], $date_array['day'], $date_array['year']);
    $datetime=new DateTime($date);    
    echo date("d/m/Y",$mktime);
    echo "<br>\r\n";
    echo $datetime->format('d/m/Y');
    

    这些是更改,但您应该能够复制并粘贴上面的代码,它会按照您想要的方式工作。

    echo date("d/m/Y",$mktime);
    echo date("m/d/Y",$mktime);
    echo $datetime->format('m/d/Y');
    echo $datetime->format('d/m/Y');
    

    参考 http://php.net/manual/en/function.date.php

    【讨论】:

    • 你在开玩笑吗?您的解决方案是手动更改日期格式...我想根据可以是任何人的用户区域设置来解析日期。在我的问题中,我编写了这段代码echo date("Y-m-d",$mktime); 以表明使用不同的语言环境,解析的日期是相同的。
    • 我认为 php 中没有一种方法可以自动化您想要自动化的内容。我认为必须手动完成。
    • 可惜了……因为相反的过程(根据用户的locale格式化日期)存在,所以php库中某处存在locale日期格式信息。
    • Javascript 有它,所以真的不需要。 javascript 中的 toLocaleString() 应该提供您正在寻找的输出。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多