Php:更改 date/strftime/strtotime 中的语言

Php: Change language in date/strftime/strtotime

我试图让我的第二段代码的输出成为荷兰语。 第一个代码给出了正确的输出,但是是英文的。我试图用荷兰语得到它,但通常我以 01-01-1970、12-12-2015 或什么都没有结束。 代码应该是本周的下周五,除非是过去的周一,否则就是下周的周五。

我的工作代码是英文的。

<?php
$d = strtotime('today');

switch ($d) {
    case strtotime('monday'):
    case strtotime('friday'):
    case strtotime('saturday'):
    case strtotime('sunday'):
        $ff = strtotime('next friday');
        $ffn = date('l d M', $ff);
        echo $ffn;
    break;
    case strtotime('tuesday'):
    case strtotime('wednesday'):
    case strtotime('thursday'):
        $ff = strtotime('next friday' . '+ 7days');
        $fft = date('l d M', $ff);
        echo $fft;
    break;
    default:
        echo "Something went wrong";
}
?>

我的代码给出了荷兰语输出,但日期错误(它给出了今天的日期,而不是下一个 friday/friday 下周。)

顺便说一句,这是我在这里问的第一个问题,所以我可能有点含糊不清或什么的;)

我现在改用 str_replace。这是(对我而言)获得我需要的输出的一种简单快捷的方法。但我现在只编程了将近一年,所以我修复它的方法可能不是最好的。但它现在有效(: 感谢回复主题的每一个人!

这就是我现在使用 str_replace 得到的结果。

<?php
$d = strtotime('today');
setlocale (LC_TIME,'NL_nl');
ini_set('intl.default_locale', 'nl_NL');
switch ($d) {
    case strtotime('monday'):
    case strtotime('friday'):
    case strtotime('saturday'):
    case strtotime('sunday'):
        $ff = strtotime('next friday');
        $ffn = date('l d F ', $ff);
        $dutch = str_replace (
            array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'),
            array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'),
            $ffn);
        echo $dutch;
    break;
    case strtotime('tuesday'):
    case strtotime('wednesday'):
    case strtotime('thursday'):
        $ff = strtotime('next friday' . '+ 7days');
        $fft = date('l d F', $ff);
        $dutch = str_replace (
            array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Friday'),
            array('Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec', 'Vrijdag'),
            $fft);
        echo $dutch;
    break;
    default:
        echo "Something went wrong";
}
?>

只需使用 strftime() 和 set_locale() 函数输出日期。如果正确,则无需更改代码逻辑。

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.setlocale.php

<?php
$d = strtotime('today');

$format = '%A %d %b';
setlocale(LC_TIME, 'NL_nl');    
setlocale(LC_ALL, 'nl_NL');

switch ($d) {
    case strtotime('monday'):
    case strtotime('friday'):
    case strtotime('saturday'):
    case strtotime('sunday'):
        $ff = strtotime('next friday');
        $ffn = strftime($format, $ff);
        echo $ffn;
    break;
    case strtotime('tuesday'):
    case strtotime('wednesday'):
    case strtotime('thursday'):
        $ff = strtotime('next friday' . '+ 7days');
        $fft = strftime($format, $ff);
        echo $fft;
    break;
    default:
        echo "Something went wrong";
}
?>

strftime() 是唯一支持本地化的 PHP 日期和时间函数。

两者 date() and strtotime() 都只使用英语。

为了得到帮助strftime()帮助你,你需要先使用setlocale()

不幸的是,setlocale()的第二个参数没有标准化,它的值在某种程度上取决于PHP代码运行s所在的操作系统。

在类 Unix 系统(Linux、OSX)上,使用:

/* Set locale to Dutch */
setlocale(LC_TIME, 'nl_NL');

/* Output: woensdag  3 juni 2015 */
echo strftime("%A %e %B %Y", strtotime('2015-06-03'));

在 Windows 上,使用:

/* Set locale to Dutch */
setlocale(LC_TIME, 'nld_nld');

以上代码片段是从setlocale()的文档中复制过来的。我更改了日期并测试了第一个示例(在 Mac OSX 上),但我手头没有 Windows 系统来检查第二个是否像宣传的那样工作。不管怎样,我可以从过去的经验中看出,你需要在 Windows 上使用的字符串与你在类 Unix 系统上使用的不同。如果您需要 运行 Windows 上的代码,您可以在 documentation 页面末尾找到有用的链接。