本文介绍了计算一周的星期数一周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个周数,例如 date -u +%W
,您如何计算从星期一开始的那一周的天数? 示例rfc -3339第40周输出:
2008-10-06
2008-10-07
2008 -10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12
解决方案
PHP
$ week_number = 40;
$ year = 2008;
($ day = 1; $ day< = 7; $ day ++)
{
回显日期('m / d / Y',strtotime($ year。W week_number。$ day))。\\\
;
}
以下帖子是因为我是个白痴没有正确地阅读这个问题,但是从星期一开始,一周内就会收到日期,而不是周数。
在PHP中改编自在:
function week_from_monday($ date){
//假设$ date的格式为DD-MM-YYYY
列表($ day,$ month,$ year )= explode( - ,$ _REQUEST [date]);
//获取给定日期的工作日
$ wkday = date('l',mktime('0','0','0',$ month,$ day, $ year));
switch($ wkday){
case'Monday':$ numDaysToMon = 0;打破;
case'Tuesday':$ numDaysToMon = 1;打破;
case'Wednesday':$ numDaysToMon = 2;打破;
case'Thursday':$ numDaysToMon = 3;打破;
case'Friday':$ numDaysToMon = 4;打破;
case'Saturday':$ numDaysToMon = 5;打破;
case'Sunday':$ numDaysToMon = 6;打破;
}
//周一星期的时间戳
$ monday = mktime('0','0','0',$ month,$ day- $ numDaysToMon,$ year);
$ seconds_in_a_day = 86400;
//从星期一(含)获取7天的日期
($ i = 0; $ i< 7; $ i ++)
{
$ [$ i] = date('Ym-d',$ monday +($ seconds_in_a_day * $ i));
}
返回$日期;
}
的输出week_from_monday('07 -10-2008 ')
给出:
Array
(
[0] => ; 2008-10-06
[1] => 2008-10-07
[2] => 2008-10-08
[3] => 2008-10- 09
[4] => 2008-10-10
[5] => 2008-10-11
[6] => 2008-10-12
)
Given a week number, e.g. date -u +%W
, how do you calculate the days in that week starting from Monday?
Example rfc-3339 output for week 40:
2008-10-06
2008-10-07
2008-10-08
2008-10-09
2008-10-10
2008-10-11
2008-10-12
解决方案
PHP
$week_number = 40;
$year = 2008;
for($day=1; $day<=7; $day++)
{
echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}
Below post was because I was an idiot who didn't read the question properly, but will get the dates in a week starting from Monday, given the date, not the week number..
In PHP, adapted from this post on the PHP date manual page:
function week_from_monday($date) {
// Assuming $date is in format DD-MM-YYYY
list($day, $month, $year) = explode("-", $_REQUEST["date"]);
// Get the weekday of the given date
$wkday = date('l',mktime('0','0','0', $month, $day, $year));
switch($wkday) {
case 'Monday': $numDaysToMon = 0; break;
case 'Tuesday': $numDaysToMon = 1; break;
case 'Wednesday': $numDaysToMon = 2; break;
case 'Thursday': $numDaysToMon = 3; break;
case 'Friday': $numDaysToMon = 4; break;
case 'Saturday': $numDaysToMon = 5; break;
case 'Sunday': $numDaysToMon = 6; break;
}
// Timestamp of the monday for that week
$monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);
$seconds_in_a_day = 86400;
// Get date for 7 days from Monday (inclusive)
for($i=0; $i<7; $i++)
{
$dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
}
return $dates;
}
Output from week_from_monday('07-10-2008')
gives:
Array
(
[0] => 2008-10-06
[1] => 2008-10-07
[2] => 2008-10-08
[3] => 2008-10-09
[4] => 2008-10-10
[5] => 2008-10-11
[6] => 2008-10-12
)
这篇关于计算一周的星期数一周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!