PHP按周数获取一周的开始和结束日期 [英] PHP get start and end date of a week by weeknumber
问题描述
我已经看到这个问题的一些变体,但我相信这个还没有被回答。
I've seen some variants on this question but I believe this one hasn't been answered yet.
我需要得到开始日期和结束日期一个星期,按年份和周数(不是日期)选择
I need to get the starting date and ending date of a week, chosen by year and week number (not a date)
示例:
input: p>
input:
getStartAndEndDate($week, $year);
输出:
$return[0] = $firstDay;
$return[1] = $lastDay;
返回值将是一个数组,其中第一个条目是星期开始日期,第二个是结束日期。
The return value will be something like an array in which the first entry is the week starting date and the second being the ending date.
可选:当我们在它时,日期格式需要是 Ynj
正常的日期格式,没有前导零。
OPTIONAL: while we are at it, the date format needs to be Y-n-j
(normal date format, no leading zeros.
我已经尝试编辑现在的功能,几乎做了我想要的,但我迄今没有运气。
I've tried editing existing functions that almost did what I wanted but I had no luck so far.
请帮助我,谢谢提前。
推荐答案
function getStartAndEndDate($week, $year)
{
$time = strtotime("1 January $year", time());
$day = date('w', $time);
$time += ((7*$week)+1-$day)*24*3600;
$return[0] = date('Y-n-j', $time);
$time += 6*24*3600;
$return[1] = date('Y-n-j', $time);
return $return;
}
这篇关于PHP按周数获取一周的开始和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!