将意大利日期时间字符串传递给日期()

时间:2014-01-05 00:29:00

标签: php sql sqlite date

我需要在SQLite表中输入DATETIME值。该日期最初以意大利月份名称发布,但格式不同(见下文)。然后我需要预先处理字符串,然后再将其发送到数据库。我之后想到使用strftime,在我指定每种格式的语言环境是意大利语之后(希望)管理不同的格式。这就是我试过的

<?php

error_reporting(E_ERROR | E_PARSE);

$dates = array("16:19, 23 set 2010 (CEST)", 
           "10:52, 9 dic 2006 (CEST)", 
           "19:38, Ago 16, 2005 (CEST)", 
           "12:34, Ago 8, 2005 (CEST)",
           "01:19, Apr 24, 2005 (CEST)");

print_r($dates);

foreach ($dates as $date) {
    setlocale(LC_TIME, "it_IT");
    $date = strftime($date);
    echo $date."\n";
    setlocale(LC_TIME, "en_AU");
    echo $date."\n";
}

?>

日期不会以时间格式转换。他们根本没有转换。这是输出:

(
    [0] => 16:19, 23 set 2010 (CEST)
    [1] => 10:52, 9 dic 2006 (CEST)
    [2] => 19:38, Ago 16, 2005 (CEST)
    [3] => 12:34, Ago 8, 2005 (CEST)
    [4] => 01:19, Apr 24, 2005 (CEST)
)
16:19, 23 set 2010 (CEST)
16:19, 23 set 2010 (CEST)
10:52, 9 dic 2006 (CEST)
10:52, 9 dic 2006 (CEST)
19:38, Ago 16, 2005 (CEST)
19:38, Ago 16, 2005 (CEST)
12:34, Ago 8, 2005 (CEST)
12:34, Ago 8, 2005 (CEST)
01:19, Apr 24, 2005 (CEST)
01:19, Apr 24, 2005 (CEST)

1 个答案:

答案 0 :(得分:1)

$date = strftime($date);

这不是此函数的正确用法,您将日期作为无效的格式传递。

strftime - 根据区域设置格式化本地时间/日期

我认为你想要的是

strftime($date_format, strtotime($date));

然而,strtotime只用英语解析日期(假设M / d / Y约定超过d / M / Y)

所以你需要先替换月份

<?php

$dates = array("16:19, 23 set 2010 (CEST)",
    "10:52, 9 dic 2006 (CEST)",
    "19:38, Ago 16, 2005 (CEST)",
    "12:34, Ago 8, 2005 (CEST)",
    "01:19, Apr 24, 2005 (CEST)");


function convert_date_to_english($date_in_italian) {

    // TODO complete this table, I don't know italian sorry
    $months = array(
        'set' => 'sep',
        'dic' => 'dec',
        'apr' => 'apr',
        'ago' => 'aug'
    );

    return str_replace(array_keys($months), array_values($months), strtolower($date_in_italian));
}


foreach ($dates as $date) {
    $date = convert_date_to_english($date);
    $timestamp = strtotime($date);

    setlocale(LC_ALL, "it_IT");
    $date = strftime("%A %e %B %Y", $timestamp);
    echo $date."\n";

    setlocale(LC_ALL, 'en_AU');
    $date = strftime("%A %e %B %Y", $timestamp);
    echo $date."\n";
}