2015-06-03 78 views
1

我试图让我的第二段代码的输出是荷兰语。 第一个代码给出正确的输出,但是用英文。我试图用荷兰语得到它,但通常我最终会与01-01-1970,12-12-2015或没有结果。 本周的代码应该给下周五,除非它的过去星期一,然后它是下周星期五。PHP:更改日期语言/ strftime/strtotime

我的工作代码,但英文。

<?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"; 
} 
?> 

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

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

+0

将您的荷兰日期转换为英文,然后使用php的strtotime函数获取下一个星期五,然后将其转换回荷兰语,也许php有一个本地化的日期;我不确定,但是您可以轻松地通过替换日/月字符串从/到荷兰语/从英语 –

+0

@NikosM。感谢您的回复,我会试一试(: – JeroenM

+0

)您可以使用strftime()以任何语言输出日期http://php.net/manual/en/function.strftime.php为什么你不返回正确的timestamp在你的switch语句中,然后用strftime()来显示荷兰语的日期? –

回答

3

只输出使用的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"; 
} 
?> 
+0

感谢,作品像一个魅力(: – JeroenM

2

现在我用str_replace代替。这对我来说是一种简单而快速的方式来获得我需要的输出。但是我现在只进行了近一年的编程,所以我的解决方法可能不是最好的。但现在工程(! 感谢eveyone谁回复了线程

这就是我得到了现在使用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"; 
} 
?> 
1

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上运行代码,您可以在documentation页面的末尾找到有用的链接。

+0

感谢您的答案,但是我用这种方法得到的问题是,我的代码没有给出正确的日期输出,但我明白,通常这种方式工作,没有搞砸结果。为我工作,没有搞乱我的结果(到目前为止)所以我认为我会继续使用str_replace。但至少我知道如何strftime()等工程(:thnx – JeroenM