简体   繁体   English

将数字转换为星期几

[英]Convert Number to Day of Week

I need to convert a number to a date ie 我需要将数字转换为日期即

1 -> Sunday
2 -> Monday

I have the following question which aids with the opposite: day of the week to day number (Monday = 1, Tuesday = 2) 我有以下相反的问题: 星期几到星期数(星期一= 1,星期二= 2)

Once I am able to do this I could for example loop through and echo the day ie: 一旦我能够做到这一点,我可以循环通过并回显当天,即:

while($i < 7)
{
    echo date("N",strtotime(1));
}

Everything I am searching for seems to be people asking to convert a day to a number. 我正在寻找的所有东西似乎都是人们要求将一天转换为数字。 Can someone please point me in the right direction 有人可以指出我正确的方向

Just put the days in an array using the day of the week as the key. 只需使用星期几作为关键字将数组放入数组中。 Then use date('N') to get the day of the week and use that as they key to access that array value. 然后使用date('N')获取星期几并使用它作为键访问该数组值。

$days = [
  1 => 'Sunday',
  2 => 'Monday',
  3 => 'Tuesday',
  4 => 'Wednesday',
  5 => 'Thursday',
  6 => 'Friday',
  7 => 'Saturday'
];

echo $days[date('N')];

You just need to create an Array to do that, Please check this : 你只需要创建一个数组就可以了,请检查一下:

$dayOfWeek = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
echo $dayOfWeek[2]."<br />";
echo $dayOfWeek[8%7];
echo $dayOfWeek[date('N')];

Why I write an array like that ? 为什么我这样写一个数组? index 0 => "Sunday" and 1 => "Monday" .... ? index 0 => "Sunday" and 1 => "Monday" .... Because if it's a day you must todo that, 1 week is 7 day so the rest you can use "Modulo / %(in php)" to convert that. 因为如果它是一天你必须todo, 1 week is 7 day所以其余你可以使用"Modulo / %(in php)"转换它。

For example : If date 1 March 2015 is Sunday so the day 8 must be Monday because 8 mod 7 is 1 or 8%7 (in php) is 1 . 例如:如果2015年3月1日是星期日,那么第8天必须是星期一, because 8 mod 7 is 1 or 8%7 (in php) is 1 So to convert that you just do this $dayOfWeek[8%7] to get the dayOfWeek. 所以转换你只需要这个$dayOfWeek[8%7]来获得dayOfWeek。 :) :)

For another conditional case you need your logic to do that. 对于另一个条件情况,您需要您的逻辑来做到这一点。 If day 1 May 2015 is Friday. 如果2015年5月1日是星期五。 Just keep in mind to check the year it was to know the February day count is 28 or 29. 请记住,检查一年是否知道2月份的天数是28或29。

How to check ? 怎么检查? please try this : 请试试这个:

$year = 2015;
$feb = (($year%100)and !($year%4)or !($year%400))+28;
echo $feb;

Day in year should like this : 日复一日应该是这样的:

$month= array(0,31,$feb,31,30,31,30,31,31,30,31,30,31);

0=0, 1=January,2=February,....

Hope this help you out.. :) 希望这可以帮到你.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM