将日期字符串从荷兰语更改为英语,以便在strtotime中使用它

时间:2017-04-06 09:40:35

标签: php date datetime localization

我有以下荷兰字符串日期:

dinsdag, 18 april 2017

现在我想剥离日期和月份,我可以通过以下示例来实现,但这不起作用,因为它在荷兰语和strtotime中仅适用于英语字符串。

$stripDay = date('d',strtotime($date));
$stripMonth = date('m',strtotime($date));

所以我检查了其他选项,我发现strptime函数表明它可以工作。

$format = '%l, %d %m %Y';
setlocale(LC_TIME, 'NL_nl');    
setlocale(LC_ALL, 'nl_NL');

首先我配置了格式,然后设置了Locale。但是,如果我使用以下代码,它仍然会让我回来。

$stripDay = date('d',strptime($date));
$stripMonth = date('m',strptime($date));

有人可以向我解释我在这里做错了吗?

2 个答案:

答案 0 :(得分:1)

你可以定义包含英语日和荷兰日的数组然后你可以用这样的英文字符串替换荷兰字符串:

<?php
    function dutch_strtotime($datetime) {
        $days = array(
            "maandag"   => "Monday",
            "dinsdag"   => "Tuesday",
            "woensdag"  => "Wednesday",
            "donderdag" => "Thursday",
            "vrijdag"   => "Friday",
            "zaterdag"  => "Saturday",
            "zondag"    => "Sunday"
        );

        $months = array(
            "januari"   => "January",
            "februari"  => "February",
            "maart"     => "March",
            "april"     => "April",
            "mei"       => "May",
            "juni"      => "June",
            "juli"      => "July",
            "augustus"  => "August",
            "september" => "September",
            "oktober"   => "October",
            "november"  => "November",
            "december"  => "December"
        );

        $array = explode(" ", $datetime);
        $array[0] = $days[strtolower($array[0])];
        $array[2] = $months[strtolower($array[2])];
        return strtotime(implode(" ", $array));
    }
    $date = "woensdag 22 oktober 2014 08:41:42";
    echo date("l d-m-Y H:i:s", dutch_strtotime($date)) . "<br />";
    echo date("d-m-Y", dutch_strtotime($date));
?>

答案 1 :(得分:0)

如果您使用$df = IntlDateFormatter::create( 'nl_NL', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Europe/Amsterdam', IntlDateFormatter::GREGORIAN, 'eeee, d MMMM yyyy' ); // since the time is omitted from the string // it uses 00:00 - which meant the timestamp // was an hour off for me in the UK so it // was rolling the date back to 23:00 the // previous day. Forcing UTC sorts this. $df->setTimeZone('Etc/UTC'); // outputs '2017-04-18' echo date('Y-m-d', $df->parse('dinsdag, 18 april 2017')); 运行PHP,则可以使用IntlDateFormatter类而不是滚动自己的翻译:

words

你可能需要ICU date format guide方便,因为它与普通 PHP日期格式不同(因此当天是'eeee')。