【发布时间】:2020-08-05 02:18:33
【问题描述】:
编辑
我在下面直接尝试了这段代码。它很接近,但仍然没有找到从每月 1 日到第 1 个星期六的周数,然后是从周日到周六之后的每周,直到最后一个周日到月底。
function get_weeks($month, $year) {
$weeks = array();
$date = DateTime::createFromFormat('mY', $month.$year);
$date->modify('first day of this month');
$end = clone $date;
$end->modify('last day of this month');
$interval = DateInterval::createFromDateString('1 week');
$period = new DatePeriod($date, $interval, $end);
$counter = 1;
foreach ($period as $dt) {
$end_of_week = clone $dt;
$end_of_week->modify('this Saturday');
$weeks[] = sprintf("Week %u: %s - %s",
$counter,
$dt->format('Y-m-d'),
$end_of_week->format('Y-m-d')
);
$counter++;
}
return $weeks;
}
$weeks = get_weeks('08', '2020');
print_r($weeks);
打印出来:
Array
(
[0] => Week 1: 2020-08-01 - 2020-08-01
[1] => Week 2: 2020-08-08 - 2020-08-08
[2] => Week 3: 2020-08-15 - 2020-08-15
[3] => Week 4: 2020-08-22 - 2020-08-22
[4] => Week 5: 2020-08-29 - 2020-08-29
)
但它应该改为打印:
Array
(
[0] => Week 1: 2020-08-01 - 2020-08-01 //correct for this month since 1st day of month and 1st Saturday of this month just happen to be on the same day
[1] => Week 2: 2020-08-02 - 2020-08-08
[2] => Week 3: 2020-08-09 - 2020-08-15
[3] => Week 4: 2020-08-16 - 2020-08-22
[4] => Week 5: 2020-08-23 - 2020-08-29
[5] => Week 6: 2020-08-30 - 2020-08-31 //last Sunday until end of month
)
原帖
我的目标是提取指定月份的指定周数的时间戳数据。我的一周从周日开始,周六结束。因此,我试图在特定月份的每周集中获取所有时间戳,以便我可以将其绘制出来,显示每周的总小时数。但首先我需要为该月指定每周的开始和结束。我可以为特定周的每个工作日执行此操作,但周/月设置对我来说太复杂了。
这是我所拥有的:
function task_range_month_week($monthweek, $cap_start_raw, $cap_end_raw) {
$start = new DateTime(date('Y-m-d',$cap_start_raw));
$end = new DateTime(date('Y-m-d',$cap_end_raw+86399));
$interval = DateInterval::createFromDateString('1 week');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
//just make it return the values for only a single (specified) week of the month
//if ($dt->format("N") == $monthweek) {
$cap_start = strtotime($dt->format("Y-m-d 00:00:00"));
$cap_end = strtotime($dt->format("Y-m-d 23:59:59"));
//}
}
然后我会这样调用函数,其中$cap_start_raw 和$cap_end_raw 是月份的开始日期和结束日期,$monthweek 是指定的周数(1-6)。因此,我试图从周日到周六(或该月的任何部分工作日)获取该特定周的时间。
$cap_start 和 $cap_end 然后将是指定周范围的输出。
我想用这个来调用那个函数:
//get the 1st week of August 2020, Sun 00:00:00 - Sat 11:59:59 (or whatever days of the month are still in this week)
task_range_month_week(1, 1596240000, 1598918399)
//this would output the 2 new timestamps for that week(1)
//$cap_start = 1XXXXXXXXX;
//$cap_end = 1XXXXXXXXX;
//get the 2nd week of August 2020, Sun 00:00:00 - Sat 11:59:59 (or whatever days of the month are still in this week)
task_range_month_week(2, 1596240000, 1598918399)
//this would output the 2 new timestamps for that week(2)
//$cap_start = 1XXXXXXXXX;
//$cap_end = 1XXXXXXXXX;
etc...
我相信它与 $dt->format() 部分有关,但不知道如何让它工作。
预期的输出将返回 $cap_start 和 $cap_end 时间戳值。
另一种说法是......
...它返回预期输出范围的新开始和结束时间戳。我为每个星期调用 task_range_month_week() 函数来查找单个星期的开始和结束($cap_start、$cap_end 输出)。因此,由于 2020 年 8 月的日期跨越 6 个不同的日历周,因此我将调用该函数 6 次,在本月的情况下,每次都会为 1-6 指定一个 $monthweek。然后在该月的第一周的情况下,新的输出范围将在该月的第一天的 00:00:00 有 $cap_start,并且由于 $cap_end 恰好落在星期六,这是我想结束我的几周(从星期日开始),它应该在同一天的 23:59:59 输出 $cap_end。然后下周将从星期日到星期六,依此类推,直到找到该月的所有一周的开始和结束时间,第一周和最后一周只有最大截止日期的范围,即月份开始和结束时间结束时间。
如果我将它设置为“1 天”而不是“1 周”,并取消注释 if ($dt->format("N") == $monthweek),那么它适用于我已经为该图表设置的指定周的天数。但是如何使它适用于周设置?
有什么想法吗?
【问题讨论】:
-
task_range_month_week()是否应该返回一个时间戳数组,范围内的每一天一个时间戳? -
@kmoser 我更新了帖子。希望这有助于更好地解释。
标签: php datetime week-number dateinterval