PHP周日数字到星期几文本

时间:2022-06-01 19:21:44

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? If I have '0' as $dow_numeric, how can I make $dow_text = 'Sun'? Yes a switch statement could do the job, but I’m looking for a more elegant solution.

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

6 个解决方案

#1


27  

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。

#2


112  

Bit of a hack, but:

有点黑客,但是:

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

#3


48  

It's not popular, but there's actually a function jddayofweek for 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.

它不受欢迎,但在PHP中实际上有一个函数jddayofweek。您可以使用第二个参数作为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,......

#4


2  

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

#5


2  

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(),它从星期一开始,而日期('w')从星期日开始:

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

#6


1  

This should work:

这应该工作:

$dow_numeric = 3;

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

#1


27  

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。

#2


112  

Bit of a hack, but:

有点黑客,但是:

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

#3


48  

It's not popular, but there's actually a function jddayofweek for 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.

它不受欢迎,但在PHP中实际上有一个函数jddayofweek。您可以使用第二个参数作为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,......

#4


2  

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

#5


2  

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(),它从星期一开始,而日期('w')从星期日开始:

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

#6


1  

This should work:

这应该工作:

$dow_numeric = 3;

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