2011-10-14 40 views
10

我需要在表单中向用户显示本地化日期名称(如'Monday','Tuesday',...)的列表。我知道得到任何日期的日期名称。但是,是否有一种特殊的防故障方式来获取数组中的全天名称?在PHP中检索日期名称

编辑:我的天名称添加到我的翻译文件,但这是很难维持的。

+0

天的名字呢?你的意思是说'星期一,星期二,星期三......等?' – LouwHopley

+2

为什么不自己定义它们?这通常是最好的方法,因为要求'date()'取决于非常不可靠的语言环境设置。 –

+2

*(参考)* http://www.php.net/manual/en/intldateformatter.format。php – Gordon

回答

13

使用strftime()结合setlocale()是一个选项。

但是你应该知道,在螺纹的PHP安装,setlocale()可以表现出乎意料,因为区域设置信息按进程保持,而不是每个线程。因此,在每次致电strftime()之前,每次拨打setlocale()以保证它使用正确的区域设置非常重要。

另外,对于Windows系统,setlocale()$locale参数需要使用一些不常用的字符串。

有关这两个问题的更多信息,请参阅文档。

像这样的东西应该工作:

// define the locales for setlocale() for which we need the daynames 
$locales = array(
    'en_EN', 
    'de_DE', 
    'nl_NL' 
    // etc... 
); 

// be aware that setlocale() needs different values on Windows machines 
// see the docs on setlocale() for more information 
$locales = array(
    'english', 
    'german', 
    'dutch' 
    // etc... 
); 

// let's remember the current local setting 
$oldLocale = setlocale(LC_TIME, '0'); 

// initialize out result array 
$localizedWeekdays = array(); 

// loop each locale 
foreach($locales as $locale) 
{ 
    // create sub result array for this locale 
    $localizedWeekdays[ $locale ] = array(); 

    // 7 days in a week 
    for($i = 0; $i < 7; $i++) 
    { 
     // set the locale on each iteration again 
     setlocale(LC_TIME, $locale); 

     // combine strftime() with the nifty strtotime() 
     $localizedWeekdays[ $locale ][] = strftime('%A', strtotime('next Monday +' . $i . ' days')); 

     // reset the locale for other threads, as a courtesy 
     setlocale(LC_TIME, $oldLocale); 
    } 
} 

// there is your result in a multi-dimensional array 
var_dump($localizedWeekdays); 
3
$dayNames = array(
    'Sunday', 
    'Monday', 
    'Tuesday', 
    'Wednesday', 
    'Thursday', 
    'Friday', 
    'Saturday', 
); 

是相当防故障:)

在一个更严重的是,PHPBB的喜好通过定义不同语言的本地化文件和硬编码做到这一点。这是真正做到这一点的唯一方法。

我会推荐下载类似的东西和看代码,看看如何做。

http://www.phpbb.com/downloads/olympus.php

+2

这很难保持所有的语言,这使我成为一只悲伤的熊猫。 –

+1

-1 OP询问本地化名称。 –

+3

他们本地化为我:) – vascowhite

32
$date = '2011/10/14'; 
$day = date('l', strtotime($date)); 
echo $day; 
+0

感谢您的好答案和简单答案+1 –

+0

工作后突然不工作,我停止了apache!它现在返回数字而不是日期名称... – KeepMove

0
setlocale(LC_TIME, 'de_DE'); 

$today = (86400 * (date("N"))); 

for($i = 0; $i < 7; $i++) { 
    $days[] = strftime('%A', time() - $today + ($i*86400)); 
} 

print_r($days); 

显然可以通过不调用时间()7倍等,但那是要点进行优化。它为您提供适合语言环境的正确语言,并保持数组的Sun-Sat顺序。

0

我知道它是一个漫长的时间,但怎么样:

$timestamp = strtotime('next Sunday'); 
$days = array(); 
for ($i = 0; $i < 7; $i++) { 
$days[] = strftime('%A', $timestamp); 
$timestamp = strtotime('+1 day', $timestamp); 
} 

how to create array of a week days name in php

+0

我没有看到此解决方案和经批准的解决方案之间的任何区别。 –

-2

此代码将工作得很好: -

$dayNames = array(
    0=>'Sunday', 
    1=>'Monday', 
    2=>'Tuesday', 
    3=>'Wednesday', 
    4=>'Thursday', 
    5=>'Friday', 
    6=>'Saturday', 
); 

现在比如你想从阵列得到任何一天: -

echo $dayNames[1]; //Output:- Monday 
2

的“通常”的方法是先从给定last day和计数在每次迭代的日子。

for ($i = 0; $i < 7; $i++) { 
    $weekDayNames[] = strftime("%a", strtotime("last sunday +$i day")); 
}