【问题标题】:How do I convert another language date format to English language format (Y-m-d)?如何将另一种语言日期格式转换为英语语言格式 (Y-m-d)?
【发布时间】:2020-05-15 10:57:41
【问题描述】:

我正在抓取一个博客站点,到目前为止一切正常,但是现在博客站点已更改为多语言,现在我在抓取日期和时间时遇到了问题。所以帮我解决这个问题。

之前我曾经通过不同的方式从博客站点获取日期格式,如下所示。

Thursday, 31 October 2019
Thursday, September 10, 2020

这对我来说很好,因为我可以通过以下方式转换为 PHP。

$fetched_date = $info['date']; // which is 'Thursday, 31 October 2019'
$time_stamp = strtotime($fetched_date);

$new_date = date('Y-m-d', $time_stamp); 
// It was gave me this date '2019-10-31',
// which is perfect for me, because i can store it to my DB.

但由于博客网站已更改为多语言,我偶尔会得到以下另一种语言的日期。那么如何转换呢?

गुरुवार, १८ जून, २०२० // mr_IN
ગુરુવાર, 10 સપ્ટેમ્બર, 2020 // gu_IN

所以当我尝试使用这种日期格式时,它给了我错误的日期,我什至无法使用。喜欢,

$fetched_date = $info['date']; // which is 'गुरुवार, १८ जून, २०२०'
$time_stamp = strtotime($fetched_date);

$new_date = date('Y-m-d', $time_stamp); 
// It was gave me this date '1970-01-01', which is wrong and i can't store to my DB.
// it should be "2020-06-18"

当我进行抓取时,我还会得到一个语言代码,例如:mr_INgu_IN,那么如何使用该语言代码获得正确的日期? 我想在 PHP 中转换日期并将其存储在 MySQL 中,我该怎么做?

【问题讨论】:

    标签: php date web-scraping country-codes


    【解决方案1】:

    IntlDateFormatter 类可以在这里提供帮助。但是,日期格式必须合适。

    $dateString = 'गुरुवार, १८ जून, २०२०';
    
    $timeZone = new DateTimeZone("UTC");
    $fmt = new IntlDateFormatter(
        'mr_IN',
        IntlDateFormatter::FULL,
        IntlDateFormatter::NONE,
        $timeZone,
        IntlDateFormatter::GREGORIAN
    );
    
    $ts = $fmt->parse($dateString);
    $dateTime = date_create("today",$timeZone)->setTimeStamp($ts);
    
    echo $dateTime->format("Y-m-d");  //2020-06-18
    

    【讨论】:

    • 为什么使用 UTC 时间?你能解释一下每一个说法吗?因为当我报废时,帖子的循环是数千次执行的,所以我不能冒险超载。如果超时,我将丢失数据。
    • 我还检查了这段代码:$fmt = new IntlDateFormatter('mr_IN', IntlDateFormatter::FULL, IntlDateFormatter::NONE); $date = date('Y-m-d', $fmt->parse($dateString));,它工作正常,那么为什么 UTC 时间在我的代码中对我有帮助,如果它已经在没有 UTC 时间和date_create() 函数的情况下工作。
    • 您可以根据需要设置时区。 UTC 只是一个例子。
    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 2017-04-19
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2012-07-21
    相关资源
    最近更新 更多