Php 一个月内的第一周和最后一周

Php 一个月内的第一周和最后一周,php,date,Php,Date,我一直在努力寻找一个月来的第一周和最后一周 $year = 2013; $month = 12; $first_week = date("W", strtotime('first week '.$year.'-'.$month)); $last_week = date("W", strtotime('last week '.$year.'-'.$month)); 结果: $first_week = 49 $last_week = 47 奇怪的结果,如何获得正确的结果?如果您指的是一个月的第

我一直在努力寻找一个月来的第一周和最后一周

$year = 2013;
$month = 12;

$first_week = date("W", strtotime('first week '.$year.'-'.$month));
$last_week = date("W", strtotime('last week '.$year.'-'.$month));
结果:

$first_week = 49
$last_week = 47

奇怪的结果,如何获得正确的结果?

如果您指的是一个月的第一个月和最后一个月所在的那一周,只需使用这些日期:

date("W", strtotime("2013-12-01"));
date("W", strtotime("2013-12-31"));
因此,在你的情况下:

$year = 2013;
$month = 12;
$daysinmonth = date("t", strtotime("$year-$month"));

$firstweek = date("W", strtotime("$year-$month-01"));
$lastweek = date("W", strtotime("$year-$month-$daysinmonth"));


您必须首先定义一个月的第几周。这是一个月的第一个星期,还是第一个完整的星期?是的,这是一个月的第一个星期在$year中,$month是变量。@Multer,应该很容易将它们变为变量。“月份格式说明符中也有天数。@MarkusKottländer否,但您可以找到它。我更新了我的问题。当我在您的示例中回显$first_week;=48美元上周;=01@Mutter,这是正确的。2013年12月31日是2014年第1周。您可以使用o来获取该周所属的年份。当我在您的示例中回显$first_week;=49美元上周;=48嗯,我在我的机器上进行了本地测试。两者都说48/01,这是正确的。
$year = 2013;
$month = 12;

$first_day = strtotime("first day of " . $year . "-" . $month);
$last_day = strtotime("last day of " . $year . "-" . $month);

$first_week = date("W", $first_day);
$last_week = date("W", $last_day);
$year = 2013;
$month = 12;

$last_day = date("t", strtotime($year . "-" . $month));

$first_week = date("W", strtotime($year . "-" . $month . "-01"));
$last_week = date("W", strtotime($year . "-" . $month . "-" . $last_day));