1 回答

TA贡献1836条经验 获得超13个赞
对于您拥有的每个时间戳 - 计算下一个时间戳(添加一周),直到它在当前时间戳之后。然后从那些中返回最低的,因为那个将是最接近现在(但也是将来)的。
假设今天是 2020 年 7 月 22 日星期三。你的 2020-07-21 星期二已经过去了,所以添加一周:2020-07-28 星期二 - 它是未来的,所以它是我们的候选者。你的 2020-07-19 星期日也已经过去了,所以添加一周:2020-07-26 星期日 - 它是未来的,所以它是第二个候选者。
现在从 2 个候选者中选择较低的:2020-07-26 星期日。
如果日期过去的时间较长,那么您将需要每周更多次。
像这样的东西:
<?php
// those are your timestamps: $timestamps = [1595289600, 1595116800];
// $time is optional int for when you want to perform the calculation. defaults to current timestamp
function nextOccurence(array $timestamps, $time = null) {
$now = $time ?? time();
$nextTimestamps = [];
foreach ($timestamps as $timestamp) {
while ($timestamp < $now) {
$timestamp += 604800;
}
$nextTimestamps[] = $timestamp;
}
return min($nextTimestamps);
}
- 1 回答
- 0 关注
- 86 浏览
添加回答
举报