在 PHP 中检索日期名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7765469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:20:14  来源:igfitidea点击:

Retrieving Day Names in PHP

phpdatelocale

提问by Cemal Eker

I need to display to user a list of localized day names (like 'Monday', 'Tuesday', ...) in a form. I know ho to get day name of any date. But is there a particular and fail-proof way to get all day names in an array.

我需要在表单中向用户显示本地化的日期名称列表(如“星期一”、“星期二”等)。我知道如何获取任何日期的日期名称。但是是否有一种特殊且防故障的方法可以在数组中获取全天名称。

Edit: I could add names of days to my translation file but that is hard to maintain.

编辑:我可以将日期名称添加到我的翻译文件中,但这很难维护。

回答by Abbas

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

回答by Decent Dabbler

Using strftime()in combination with setlocale()is an option.

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

However you should be aware that on threaded php installs, setlocale()can behave unexpected, since locale information is maintained per process, not per thread. Therefor it is important to call setlocale()every time before each call to strftime()to guarantee it uses the correct locale.

但是,您应该知道,在线程化 php 安装中,setlocale()可能会出现意外情况,因为区域设置信息是按进程维护的,而不是按线程维护的。因此,setlocale()在每次调用之前调用strftime()以确保它使用正确的语言环境非常重要。

Also, for Windows systems, you need to use somewhat unusual strings for the $localeparameter for setlocale().

此外,对于 Windows 系统,您需要$localesetlocale().

See the docs for more information on both of these issues.

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

Something like this should work:

这样的事情应该工作:

// 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 );

回答by vascowhite

$dayNames = array(
    'Sunday',
    'Monday', 
    'Tuesday', 
    'Wednesday', 
    'Thursday', 
    'Friday', 
    'Saturday', 
 );

Is pretty fail-proof :)

非常防故障:)

On a more serious note, the likes of PHPBB do this by defining localization files and hard coding for different languages. It's really the only way to do this reliably.

更严肃地说,PHPBB 之类的软件通过为不同语言定义本地化文件和硬编码来实现这一点。这确实是可靠地做到这一点的唯一方法。

I would recommend downloading something like that and looking at the code to see how its done.

我建议下载类似的东西并查看代码以了解它是如何完成的。

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

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

回答by yckart

The "usual" way is to start with a given last dayand count up a day on each iteration.

“通常”的方法是从给定的开始,last day并在每次迭代中计算一天。

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

回答by d0001

I know its been a long time, but what about:

我知道它已经很长时间了,但是呢:

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

from how to create array of a week days name in php

如何在 php 中创建工作日名称数组

回答by Ben Swinburne

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);

Obviously can be optimised by not calling time() 7 times etc but that's the gist. It gives you the right language for the locale and keeps Sun-Sat order of the array in tact.

显然可以通过不调用 time() 7 次等来优化,但这就是要点。它为您提供适合区域设置的正确语言,并保持阵列的 Sun-Sat 顺序。

回答by Dinesh Rawat

This code will work pretty well:-

此代码将工作得很好:-

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

Now for example you wants to get the any day from array:-

现在,例如您想从数组中获取任何一天:-

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