PHP 星期几数字到星期几文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4742354/
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-25 14:12:44  来源:igfitidea点击:

PHP day of week numeric to day of week text

phpdate

提问by csi

This may be really easy but I can't find a PHP function to do this...

这可能真的很简单,但我找不到 PHP 函数来执行此操作...

OK so

好吧

$dow_numeric = date('w');

gives the numeric day of the week 0-6 for Sunday to Saturday.

给出星期日到星期六的星期数 0-6。

And

$dow_text = date('D');

gives the 3 letter abbreviation for the text day of the week (Sun, Mon, etc.)

给出一周中文本日期的 3 个字母缩写(Sun、Mon 等)

Is there a function or easy way to use $dow_numericto get $dow_text? If I have '0' as $dow_numeric, how can I make $dow_text = 'Sun'? Yes a switchstatement could do the job, but I'm looking for a more elegant solution.

有没有函数或简单的方法可以$dow_numeric用来获取$dow_text?如果我有 '0' as $dow_numeric,我该怎么做$dow_text = 'Sun'?是的,switch声明可以完成这项工作,但我正在寻找更优雅的解决方案。

采纳答案by ThiefMaster

Create an array to map numeric DOWs to text DOWs.

创建一个数组以将数字 DOW 映射到文本 DOW。

$dowMap = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');

If you need locale support, load the dow of some random date (epoch (0) would be a good date for example) and then for the next 6 days and build the dow map dynamically.

如果您需要语言环境支持,请加载某个随机日期的 dow(例如,epoch (0) 将是一个很好的日期),然后在接下来的 6 天内动态构建 dow 地图。

回答by Hamish

Bit of a hack, but:

有点黑客,但是:

$dow_text = date('D', strtotime("Sunday +{$dow_numeric} days"));

回答by Chibueze Opata

It's not popular, but there's actually a function jddayofweekfor this in PHP. You can call it with the second parameter as 1 to get full gregorian week day name or 2 for the abbreviated name.

它并不流行,但实际上jddayofweek在 PHP 中有一个函数。您可以使用第二个参数作为 1 来调用它以获得完整的公历星期名称或 2 作为缩写名称。

e.g. jddayofweek(2, 2); #returns Wed

Note that for this function, numbers start at Monday. So Monday=0, Tuesday=1, ...

请注意,对于此函数,数字从星期一开始。所以星期一=0,星期二=1,...

回答by Reza Mamun

To get Sunday to Saturdayfrom numeric day of the week 0 to 6:

要从星期0 到 6 的数字日期获得星期日到星期六

//For example, our target numeric day is 0 (Sunday):
$numericDay = 0; //assuming current date('w')==0 and date('D')=='Sun';

Solution-1:Using PHP's built-in function jddayofweek()which starts from Mondaywhereas date('w')starts from Sunday:

解决方法1:使用PHP的内置函数 jddayofweek()从开始Monday,而date('w')从开始Sunday

jddayofweek($numericDay-1, 1); //returns 'Sun', here decreasing by '-1' is important(!)

//jddayofweek(0, 1); //returns 'Mon';
//jddayofweek(0, 2); //returns 'Monday';

Solution-2:Using a trick(!):

解决方案 2:使用技巧(!):

date('D', strtotime("Sunday +{$numericDay} days")); //returns 'Sun';

//date('l', strtotime("Sunday +{$numericDay} days")); //returns 'Sunday';

回答by Azaz Khan

function getDay(){
    $dowMap = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    $dow_numeric = date('w');
    return $dowMap[$dow_numeric];
}

回答by CodeBrauer

If you need locale support you can use a bit improved function of Hamishs answer:

如果您需要语言环境支持,您可以使用Hamishs 答案的一些改进功能:

Example using german weekdays:

使用德国工作日的示例:

setlocale(LC_TIME, 'de_DE');
$dow_numeric = 3;
$dow_text = strftime('%A', strtotime("Sunday +{$dow_numeric} days"));

http://php.net/strftime

http://php.net/strftime

If $dow_numericstart on 0(0= Monday) you can just change Sundayto Mondayor add $dow_numeric += 1

如果$dow_numeric00= 星期一)开始,您可以更改SundayMonday或添加$dow_numeric += 1

回答by Matt Lowden

This should work:

这应该有效:

$dow_numeric = 3;

$last_sunday = strtotime('last Sunday');
$dow_text = date('D', strtotime('+'.$dow_numeric.' day', $last_sunday));