我有一个单词数组,每个单词从当天下午 5 点到第二天下午 5 点都有效,然后下一个单词有效。除了周末,周五的一个词持续到周一下午 5 点。
现在我要做的是确定用户输入的单词在这段时间内是否有效。我有一个工作正常的示例,但我的问题是周末把一切都搞砸了。而且我似乎无法弄清楚如何使它们起作用。
我有一个函数来计算两个时间戳之间有多少个周末
// Figure out how many weekends occured between two dates
function weekends($date1, $date2) {
$d1 = new DateTime('@' . $date1);
$d2 = new DateTime('@' . $date2);
// Compare the two dates
$diff = $d1->diff($d2);
// Get number of days between them
$days = $diff->format('%a');
// Find out day of the week we start on
$start = $d1->format('w');
// Verify we are not on a weekend
if($start == 0 || $start == 6)
return false;
// Number of days until weekend
$until_weekend = 7 - $start; // (6 is Saturday but we are counting today)
// Find out how many days are left between the first weekend and the end
$left = $days - $until_weekend;
// How many weekends
$weekends = floor($left / 7) + 1;
return $weekends;
}
然后我得到了一个函数来确定这个词在那个日期范围内是否有效
// Keyword Validation
function keyword_validate($keywords = array()) {
if(empty($keywords)) return false;
// Break into values
$keyword = $keywords['keyword'];
$contest = $keywords['contest'];
$keywords = $contest['keywords'];
// Get some dates
$now = new DateTime('now');
$start = new DateTime('@' . $contest['start_time']);
$s1 = new DateTime('@' . $contest['start_time']); // value for timestamps
$s2 = new DateTime('@' . $contest['end_time']); // value for timestamps
$end = new DateTime('@' . $contest['end_time']);
// Verify keyword exists
if(in_array($keyword, $keywords) === FALSE)
return false;
// Get index
$index = array_search($keyword, $keywords);
// See if we somehow got more then one keyword
if(count($index) != 1)
return false;
// get number of weekends
$weekends = weekends($start->getTimestamp(), $end->getTimestamp());
// Based on index get the two container timestamps
$s = $s1->add(new DateInterval('P' . $index + $weekends . 'D'));
// Verify start doesn't equal Friday or a Weekend
$e = $s2->add(new DateInterval('P' . $index + $weekends + 1 . 'D'));
if($s === FALSE || $e === FALSE)
return false; // Something really bad happened
// Based on dates find out if the keyword works.
print $s->getTimestamp();
print $e->getTimestamp();
// Get the current time
}
如您所见,关键字函数在 atm 中不起作用。我正在做的 atm 是将关键字的索引与一天相匹配,但是如果是星期二(两个周末之后),我怎么能做到这一点,所以索引增加了 4。对不起,如果这没有任何意义,我我有点失落。