2 回答
TA贡献1875条经验 获得超5个赞
如果您有一个由$day,$month和指定的日期year,要获取“下一个{工作日}”,例如“下周六”,您可以使用以下方式:
echo date("Y-m-d",strtotime("next saturday",strtotime("$month/$day/$year")));
请注意,在某些情况下,诸如02/09和09/02之类的日期都是合理的,因此您可能会遇到问题,具体取决于您的strtotime理解。
您可以用其他一些不错的东西替换“下周六”,例如:
strtotime("now");
strtotime("10 September 2000");
strtotime("+1 day");
strtotime("+1 week");
strtotime("+1 week 2 days 4 hours 2 seconds");
strtotime("next Thursday");
strtotime("last Monday");
更多信息在这里: https: //www.php.net/manual/en/function.strtotime.php
TA贡献1843条经验 获得超7个赞
我写了下面的函数来实现它。并且它在初始测试中运行良好。
$Day_Config = array(
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday'
);
$Week_Config = array(
1 => 'first',
2 => 'second',
3 => 'third',
4 => 'fourth',
5 => 'fifth',
);
function weekOfMonth($date) {
#Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", strtotime($date)));
#Apply formula.
return intval(date("W", strtotime($date))) - intval(date("W", $firstOfMonth)) + 1;
}
function getDateFromText ($year,$month,$week,$day) {
global $Day_Config;
global $Week_Config;
$txtFormat = $Week_Config["$week"]." ".strtolower($Day_Config["$day"])." of ".date("F", mktime(0, 0, 0, $month, 10))." ".$year;
$DateFromTtxt = date('Y-m-d', strtotime("$txtFormat"));
return $DateFromTtxt;
}
function getNextRuntime ($day,$week,$month,$time) {
#Get Current Day Details
$Year = date("Y");
$thisMonth = date("n");
$thisWeek = weekOfMonth(date("Y-m-d"));
$thisDay = date('N');
#Get Received Data From DB
$Days = explode(",",$day);
$Weeks = explode(",",$week);
$Months = explode(",",$month);
#Loop Through Months
foreach ($Months as $Value_Month){
if($Value_Month >= $thisMonth){
#Loop Through Weeks
foreach ($Weeks as $Value_Week){
if($Value_Month == $thisMonth){
if($Value_Week >= $thisWeek){
#Loop Through Days
foreach ($Days as $Value_Day){
if($Value_Month == $thisMonth && $Value_Week == $thisWeek){
if($Value_Day >= $thisDay){
if($Value_Day == $thisDay && $time > date('H:i:00')){
return getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
} elseif ($Value_Day != $thisDay) {
$Result = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
$Result_Month = date('n', strtotime($Result));
if($Result_Month == $Value_Month){
return $Result;
}
}
}
} else {
$Result = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
$Result_Month = date('n', strtotime($Result));
if($Result_Month == $Value_Month){
return $Result;
} else {
break;
}
}
}
}
} else {
foreach ($Days as $Value_Day){
$Result = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
$Result_Month = date('n', strtotime($Result));
if($Result_Month == $Value_Month){
return $Result;
} else {
break;
}
}
}
}
}
}
#Select Next Year If Current Year Not Provided Any Date
++$Year;
foreach ($Months as $Value_Month){
foreach ($Weeks as $Value_Week){
foreach ($Days as $Value_Day){
$Result = getDateFromText($Year, $Value_Month, $Value_Week, $Value_Day);
$Result_Month = date('n', strtotime($Result));
if($Result_Month == $Value_Month){
return $Result;
} else {
break;
}
}
}
}
return false;
}
#Testing Functions
$day = '2,5,7';
$week = '2,5';
$month = '9,10';
$time = '10:00:00';
echo getNextRuntime ($day,$week,$month,$time)
#Output
If you are running it on 13th September 2020 before 10:00 AM, then the output will be as below
```2020-09-13```
- 2 回答
- 0 关注
- 119 浏览
添加回答
举报