用户选择一个特定的月份。一个函数搜索和设计一个特定的时间段。从月的第一天到星期六,星期日-星期六,直到最后一周。星期日-月的最后一天
例如:
当参数为2022-11时
2022-11-01 - 2022-11-05
2022-11-06 - 2022-11-12
...
2022-11-27 -2022-11-30我的代码不能正常工作。我该怎么修呢?
我认为
date($arraySaturdayList[$k], strtotime("+1 day")似乎不太好用。
private function month($start_date,$end_datel,holidays,
{
$arraySaturdayList = $this->makeSaturDayList($start_date,$end_date);
/*
$arraySaturdayList = Array
(
[0] => 2022-11-05
[1] => 2022-11-12
[2] => 2022-11-19
[3] => 2022-11-26
)
*/
for($i = $start_date,$k=0; $i <= $end_date ;$i++)
{
if($k+1 > sizeof($arraySaturdayList))
{
break;
}
$previousSunday = "";
$nextSunday = date($arraySaturdayList[$k], strtotime("+1 day"));
if($k > 0){
$previousSunday = date($arraySaturdayList[$k-1], strtotime("+1 day"));
}
}
//2nd week~ NOT the final
else if($k < sizeof($arraySaturdayList)){
//start_date should start from sunday . end_date should be ended in saturday.
$this->assignByWeekRange($previousSunday,$arraySaturdayList[$k]);
}
//the last week. (start from sunday end should be the last day of a month.)
else{
$this->assignByWeekRange($previousSunday,$end_date);
}
$k++;
}
}结果是
S=2022-11-01E=2022-11-05
S=2022-11-05E=2022-11-12
S=2022-11-12E=2022-11-19
S=2022-11-19E=2022-11-26 发布于 2022-05-31 12:47:42
将以下代码放入定义的文件中,并对它们进行测试。
expectedWeeks.php
<?php
$weeksByMonths = [
"01" => [
"2022-01-01 - 2022-01-01",
"2022-01-02 - 2022-01-08",
"2022-01-09 - 2022-01-15",
"2022-01-16 - 2022-01-22",
"2022-01-23 - 2022-01-29",
"2022-01-30 - 2022-01-31"
],
"02" => [
"2022-02-01 - 2022-02-05",
"2022-02-06 - 2022-02-12",
"2022-02-13 - 2022-02-19",
"2022-02-20 - 2022-02-26",
"2022-02-27 - 2022-02-28"
],
"03" => [
"2022-03-01 - 2022-03-05",
"2022-03-06 - 2022-03-12",
"2022-03-13 - 2022-03-19",
"2022-03-20 - 2022-03-26",
"2022-03-27 - 2022-03-31"
],
"04" => [
"2022-04-01 - 2022-04-02",
"2022-04-03 - 2022-04-09",
"2022-04-10 - 2022-04-16",
"2022-04-17 - 2022-04-23",
"2022-04-24 - 2022-04-30"
// ,"2022-05-01 - 2022-04-30"
],
"05" => [
"2022-05-01 - 2022-05-07",
"2022-05-08 - 2022-05-14",
"2022-05-15 - 2022-05-21",
"2022-05-22 - 2022-05-28",
"2022-05-29 - 2022-05-31"
],
"06" => [
"2022-06-01 - 2022-06-04",
"2022-06-05 - 2022-06-11",
"2022-06-12 - 2022-06-18",
"2022-06-19 - 2022-06-25",
"2022-06-26 - 2022-06-30"
],
"07" => [
"2022-07-01 - 2022-07-02",
"2022-07-03 - 2022-07-09",
"2022-07-10 - 2022-07-16",
"2022-07-17 - 2022-07-23",
"2022-07-24 - 2022-07-30",
"2022-07-31 - 2022-07-31"
],
"08" => [
"2022-08-01 - 2022-08-06",
"2022-08-07 - 2022-08-13",
"2022-08-14 - 2022-08-20",
"2022-08-21 - 2022-08-27",
"2022-08-28 - 2022-08-31"
],
"09" => [
"2022-09-01 - 2022-09-03",
"2022-09-04 - 2022-09-10",
"2022-09-11 - 2022-09-17",
"2022-09-18 - 2022-09-24",
"2022-09-25 - 2022-09-30"
],
"10" => [
"2022-10-01 - 2022-10-01",
"2022-10-02 - 2022-10-08",
"2022-10-09 - 2022-10-15",
"2022-10-16 - 2022-10-22",
"2022-10-23 - 2022-10-29",
"2022-10-30 - 2022-10-31"
],
"11" => [
"2022-11-01 - 2022-11-05",
"2022-11-06 - 2022-11-12",
"2022-11-13 - 2022-11-19",
"2022-11-20 - 2022-11-26",
"2022-11-27 - 2022-11-30"
],
"12" => [
"2022-12-01 - 2022-12-03",
"2022-12-04 - 2022-12-10",
"2022-12-11 - 2022-12-17",
"2022-12-18 - 2022-12-24",
"2022-12-25 - 2022-12-31"
//,"2023-01-01 - 2022-12-31"
],
];helperFunctions.php
<?php
function printList($list)
{
echo "\n\n";
foreach($list as $entry)
{
echo "\n". implode(" - ", $entry);
}
}
function compareResult($expected, $generated)
{
if(count($expected) != count($generated))
{
echo "\n\nERROR: The number of generated ranges differ from expected one";
print_r($expected);
printList($generated);
return;
}
foreach($generated as $i => $entry)
{
if($expected[$i] != implode(" - ", $entry))
{
echo "\n\nERROR: The generated range $entry differs from the expected one: " . $expected[$i];
print_r($expected);
printList($generated);
return;
}
}
echo "\n\nSUCCESS: Generated ranges equal to expected ones.";
}最后是主脚本main.php ,其中包含生成范围.的函数。
<?php
include "expectedWeeks.php";
include "helperFunctions.php";
define("DAY_SECONDS" , 86400);
define("LOG_STEPS_TO_CONSOLE" , true);
/**
* string $yearMonth Month "2022-11
*
*/
function getAllSaturdays($yearMonth, $asDate = false)
{
$time = strtotime($yearMonth);
$month = intval(date("n", $time));
$currentMonth = $month;
$saturdays = [];
while($currentMonth == $month)
{
if(date("D", $time) == "Sat")
{
$saturdays[] = $asDate ? date("Y-m-d", $time) : $time;
}
$time += DAY_SECONDS;
$currentMonth = intval(date("n", $time));
}
return $saturdays;
}
/**
* Generates a list of ranges, which start with Sunday and end with Saturday by default,
* otherwise starts with the first day and/or resp. ends with last day of the montch of the month.
*
*/
function generateWeeksEndWithSaturday($yearMonth)
{
$list = [];
if(LOG_STEPS_TO_CONSOLE) echo "\n\nMONTH: " . date("F", strtotime($yearMonth));
$saturdays = getAllSaturdays($yearMonth);
$nDaysInMonth = date("t", strtotime($yearMonth));
foreach($saturdays as $saturdayTime)
{
$saturdayTime += 43200;// midday time for correct calculation on time shifting days
$day = intval(date("j", $saturdayTime));
$saturDate = date("Y-m-d", $saturdayTime);
if(LOG_STEPS_TO_CONSOLE) echo "\n\nSATURDAY : " . $saturDate;
# This Saturday is the first day of the month
if($day == 1)
{
if(LOG_STEPS_TO_CONSOLE) echo "\nFirst day of the month";
$list[] = [$saturDate, $saturDate];
if(LOG_STEPS_TO_CONSOLE) echo "\nAdding: " . implode(":", $list[array_key_last($list)]);
}
# This Saturday is the 6th day of the month
else if($day < 7)
{
if(LOG_STEPS_TO_CONSOLE) echo "\t-->\tWithin the first 6 days";
$from = date("Y-m-d", $saturdayTime - (($day-1) * DAY_SECONDS));
$list[] = [$from, $saturDate];
if(LOG_STEPS_TO_CONSOLE) echo "\nAdding: " . implode(" - ", $list[array_key_last($list)]);
}
#
elseif($day+7 > $nDaysInMonth)
{
if(LOG_STEPS_TO_CONSOLE) echo "\t-->\tThe last saturday in the month";
// current week
$from = date("Y-m-d", $saturdayTime - (6*DAY_SECONDS));
// $from = date("Y-m-d", $saturdayTime - (6*86400));
$list[] = [$from, $saturDate ];
if(LOG_STEPS_TO_CONSOLE) echo "\nAdding: " . implode(" - ", $list[array_key_last($list)]);
# The rest of the month
if($day < $nDaysInMonth)
{
$n = $nDaysInMonth - $day;
$from = date("Y-m-d", $saturdayTime + DAY_SECONDS);
$to = date("Y-m-d", $saturdayTime + ($n*DAY_SECONDS));
$list[] = [$from, $to];
if(LOG_STEPS_TO_CONSOLE) echo "\nAdding the rest of the month: " . implode(" - ", $list[array_key_last($list)]);
}
}
else
{
if(LOG_STEPS_TO_CONSOLE) echo "\t-->\tNormal";
$from = date("Y-m-d", $saturdayTime - (6*DAY_SECONDS));
$list[] = [$from, $saturDate];
if(LOG_STEPS_TO_CONSOLE) echo "\nAdding: " . implode(" - ", $list[array_key_last($list)]);
}
}
return $list;
}
// Month hard coded
$month = "11";
// Pass the month as parameter. For example: php weeks.php 11
$month = $argv[1];
compareResult($weeksByMonths[$month], generateWeeksEndWithSaturday("2022-$month"));然后,您可以在控制台中执行,如下所示:
php main.php 05您应该使用输出:

泛型方法,适用于每周的所有工作日
您可以将函数getAllSaturdays()重命名为泛型函数,并将其作为范围内的结束日使用一周:
function getAllDatesForWeekdayInAMonth($yearMonth, $weekDay, $asDate = false)
{
$time = strtotime($yearMonth);
$month = intval(date("n", $time));
$currentMonth = $month;
$days = [];
while($currentMonth == $month)
{
if(date("D", $time) == $weekDay)
{
$days[] = $asDate ? date("Y-m-d", $time) : $time;
}
$time += DAY_SECONDS;
$currentMonth = intval(date("n", $time));
}
return $days;
}https://stackoverflow.com/questions/72443431
复制相似问题