【问题标题】:Figure out what day each array starts and ends on?弄清楚每个数组在哪一天开始和结束?
【发布时间】:2013-01-17 18:26:39
【问题描述】:

我有一个单词数组,每个单词从当天下午 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 是将关键字的索引与 day 匹配,但如果是星期二(2 个周末后),我该怎么做才能使索引增加 4。对不起,如果这没有任何意义,我有点失落。

【问题讨论】:

    标签: php


    【解决方案1】:

    尝试重新定义问题以使其更简单。与其尝试用有趣的异常进行数学运算来确定哪个项目与哪一天相关联,不如尝试创建一个新数组,其中包含每天的单词值。它可能看起来像这样:

    array(
        'cake',
        'pie',
        'pudding',
        'happy', //friday
        'happy', //saturday
        'happy', //sunday
        'nothappy',
        ...
    );
    

    构建这个数组应该比您现在尝试做的事情更简单。一旦你有了这个数组,检查应该是微不足道的。

    【讨论】:

      【解决方案2】:

      难道你不能只拥有一个可以包含七个空格的数组吗?当“friday”索引发生变化时,设置“Saturday”/“Sunday”来镜像它。

      【讨论】:

        猜你喜欢
        • 2012-10-30
        • 2012-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-02
        相关资源
        最近更新 更多