在给定年份的某周,获取该周第一天的日期(格式为“Y-m-d”)。涉及IT技术。

4

我正在试图根据输入的周数和年份获取给定周的第一天。目前在当前周和年份中使用此方法,效果非常好。

$week = date('W');
$year = date('Y');
$date1 = date('Y-m-d', strtotime($year."W".$week.'1'));

然而,当我尝试输入自己的周和年时,它无法正常工作。

$week = 7;
$year = 2013;
$date1 = date('Y-m-d', strtotime($year."W".$week.'1'));

我该使用什么样的字符串来进行strtotime操作?为什么提供自己的日期不起作用?


1
https://dev59.com/oHI-5IYBdhLWcg3wW2-p - fionbio
请参考以下链接:https://dev59.com/gW7Xa4cB1Zd3GeqPmB_3 - ibrahim.alzein
以下是我编写的代码,用于获取给定周的第一天和最后一天:$string = $year.'W'.str_pad($week, 2, '0', STR_PAD_LEFT); $date1 = date('Y-m-d', strtotime($string.'1')); $date2 = date('Y-m-d', strtotime($string.'7')); - seesoe
2
我不认为这是重复的。链接的问题需要从特定日期获取一周数字,而这个问题则相反。 - Sergio
请注意,“周数”在不同的语言环境下是不同的:http://www.pjh2.de/datetime/weeknumber/wnd.php?l=en#Legend - Dima Tisnek
1
这个问题并不是一个重复的问题,而且https://dev59.com/oHI-5IYBdhLWcg3wW2-p并没有提供如何从一周的数字中获取第一天日期的答案。 - bancer
1个回答

7

因为周数应该是填充的(而末尾的多余1实际上并没有什么作用):

$date1 = date(
    'Y-m-d', 
    strtotime($year . 'W' . str_pad($week, 2, '0', STR_PAD_LEFT))
);

完美,我以为与那有关,谢谢! - seesoe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接