只能输入0和1,最大336位;
按照特定时间段时,01010100010010……表示指定特殊时段,半小时为粒度,用0、1标识是否选中,1为选中,0为未选中。一天24小时,即每天24 * 2=48位,共7天,即共48 * 7=336位;
//每天的9点到20点投放
$week = Carbon::parse('2024-05-29')->dayOfWeek;
dd($week, $this->getPromoteTimeRangePeriod(['12:00:00','24:00:00']));
//输出:
//000000000000000000000000111111111111111111111111000000000000000000000000111111111111111111111111000000000000000000000000111111111111111111111111000000000000000000000000111111111111111111111111000000000000000000000000111111111111111111111111000000000000000000000000111111111111111111111111000000000000000000000000111111111111111111111111
//拉空 2024-05-29 这一日12点到24点
$week = Carbon::parse('2024-05-29')->dayOfWeek;
dd($week, $this->getPromoteTimeRangePeriod(['12:00:00','24:00:00'], '0', $week));
//输出:
//111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
/**
* 获取半小时粒度时间段
*
* @param $range
* @param $selectedString | 选中时段填充值
* @param $week | 指定星期几
* @return string
*/
public function getPromoteTimeRangePeriod($range = [], $selectedString = '1', $week = null) {
$selectionString = ''; // 初始化为空字符串
$halfHoursPerDay = 48; // 每天的半小时段数
$fullString = $selectedString == 1 ? '0' : '1'; //未选中时段填充值
$startHour = Carbon::parse($range[0])->format('H');
$endHour = Carbon::parse($range[1])->format('H');
if($endHour == '00') {
$endHour = 24;
}
// 遍历7天
for ($day = 1; $day <= 7; $day++) {
// 遍历每天的每半小时
for ($halfHour = 0; $halfHour < $halfHoursPerDay; $halfHour++) {
$halfHourValue = floor($halfHour/2);
if($week && $week != $day) {
$isSelected = false;
} else{
$isSelected = $halfHourValue >= $startHour && $halfHourValue < $endHour;
}
$selectionString .= $isSelected ? $selectedString : $fullString;
}
}
return $selectionString;
}