[英]PHP day of week numeric to day of week text
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_numeric
to get $dow_text
? 是否有一个函数或简单的方法来使用$dow_numeric
来获取$dow_text
? If I have '0' as $dow_numeric
, how can I make $dow_text = 'Sun'
? 如果我将'0'作为$dow_numeric
,我怎样才能使$dow_text = 'Sun'
? Yes a switch
statement could do the job, but I'm looking for a more elegant solution. 是的, switch
语句可以完成这项工作,但我正在寻找更优雅的解决方案。
有点黑客,但是:
$dow_text = date('D', strtotime("Sunday +{$dow_numeric} days"));
It's not popular, but there's actually a function jddayofweek
for this in PHP. 它不受欢迎,但在PHP中实际上有一个函数jddayofweek
。 You can call it with the second parameter as 1 to get full gregorian week day name or 2 for the abbreviated name. 您可以使用第二个参数作为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,......
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 map。
To get Sunday to Saturday from 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 Monday
whereas 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';
function getDay(){
$dowMap = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$dow_numeric = date('w');
return $dowMap[$dow_numeric];
}
This should work: 这应该工作:
$dow_numeric = 3;
$last_sunday = strtotime('last Sunday');
$dow_text = date('D', strtotime('+'.$dow_numeric.' day', $last_sunday));
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_numeric
start on 0
( 0
= Monday) you can just change Sunday
to Monday
or add $dow_numeric += 1
如果$dow_numeric
从0
( 0
=星期一)开始,您可以将Sunday
更改为Monday
或添加$dow_numeric += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.