获得一个月的第一周和最后一周

时间:2013-12-29 18:29:47

标签: php date

我一直试图用strtotime找到一个月的第一周和最后一周

$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

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

2 个答案:

答案 0 :(得分:0)

如果您的意思是“本月的第一个和最后一个星期”,请使用这些日期:

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"));

答案 1 :(得分:0)

$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));