Php:更改日期/ strftime / strtotime 中的语言

3
我正在尝试让我第二段代码的输出变成荷兰语。第一段代码给出了正确的输出,但是是英文。我试图将其转换为荷兰语,但通常最终会得到1970年1月1日、2015年12月12日或什么都没有的结果。该代码应该给出本周的下一个星期五,除非已经过了星期一,那么就是下周的星期五。
我的可用代码,但是是英文。
<?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";
}
?>

我的代码输出是荷兰语,但日期不对(它给出的是今天的日期,而不是下周五/下周的星期五)。

顺便说一下,这是我第一次在这里提问,所以可能会有点含糊或其他问题;)


将您的荷兰日期转换为英文,然后使用PHP的strtotime函数获取下一个星期五,然后将其转换回荷兰日期。也许PHP有本地化日期,我不确定,但是您可以通过将日/月字符串从荷兰语替换为英语,或者从英语替换为荷兰语来轻松创建本地化日期。 - Nikos M.
@NikosM。感谢您的回复,我会尝试一下(: - JeroenM
你可以使用strftime()在任何语言中输出日期。http://php.net/manual/en/function.strftime.php 为什么你不在switch语句中返回正确的时间戳,然后使用它与strftime()一起显示荷兰日期? - Yasen Zhelev
@YasenZhelev 我已经使用过了,但不知何故我的输出出了问题(但语言确实改变了)。但还是谢谢你的回复 (: - JeroenM
3个回答

5

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

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

http://php.net/manual/zh/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";
}
?>

我正在尝试在我的Laravel Blade视图中以法语显示日期,这是我的代码 "<?php setlocale(LC_TIME, 'fr_FR'); echo strftime("%d %B %G", strtotime($lastNews->created_at)); ?> ,但它仍然以英语显示。" - Youssef Boudaya

3

现在我使用了 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";
}
?>

2

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

date()strtotime() 只使用英语。

为了使用 strftime(),你需要先使用 setlocale()

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

在类 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上运行代码,您可以在文档页面底部找到有用的链接。

谢谢你的回答,但是我用这种方法遇到的问题是我的代码没有给出正确的日期输出。不过我知道通常情况下这种方法可以工作而不会弄乱结果。str_replace 对我有用,而且没有改变我的结果(至少目前是这样)。所以我想我还是会继续使用 str_replace。但至少现在我知道了 strftime() 等函数是如何工作的 (: 谢谢 - JeroenM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接