1

在过去的几天里,我一直在断断续续地处理这段代码,但无法弄清楚。

我需要做的是从函数返回 0 或 1,具体取决于当前时间是否在用户设置的时间范围内。如果时间和日期在用户设置的4值数组内,则返回1,否则返回0。用户可以设置多个数组,用于多个时间段。

我一直在尝试使用此代码一段时间:

函数.php:

function determineWoE($woe) {
  $curDayWeek = date('N');
  $curTime = date('H:i');
  $amountWoE = count($woe['WoEDayTimes']); // Determine how many WoE times we have.
  if ( $amountWoE == 0 ) {
    return 0; // There are no WoE's set! WoE can't be on!
  }
  for ( $i=0; $i < $amountWoE; $i++ ) {
    if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) { // Check the day of the week.
      if ( $woe['WoEDayTimes'][$i][1] >= $curTime && $woe['WoEDayTimes'][$i][3] <= $curTime ) { // Check current time of day.
        // WoE is active
        return 1;
      }
      else {
        // WoE is not active
        return 0;
      }
    }
    else {
      // WoE is not active
      return 0;
    }
  }
}

并且...用户可以根据需要为该功能设置任意多的时间段:

$woe = array( // Configuration options for WoE and times.
  // -- WoE days and times --
  // First parameter: Starding day 1=Monday / 2=Tuesday / 3=Wednesday / 4=Thursday / 5=Friday / 6=Saturday / 7=Sunday
  // Second parameter: Starting hour in 24-hr format.
  // Third paramter: Ending day (possible value is same or different as starting day).
  // Fourth (final) parameter: Ending hour in 24-hr format.
  'WoEDayTimes'   => array(
    array(6, '18:00', 6, '19:00'), // Example: Starts Saturday 6:00 PM and ends Saturday 7:00 PM
    array(3, '14:00', 3, '15:00')  // Example: Starts Wednesday 2:00 PM and ends Wednesday 3:00 PM
  ),
);

但是,无论我做什么......函数determineWoE总是返回0。

我是否需要在函数中使用 foreach 而不是 for?如果时间在用户可设置的时间内,我如何让确定WoE 返回 1?

尝试将 for 更改为 foreach:

foreach ( $woe['WoEDayTimes'] as $i ) {

现在我收到错误:警告:第 76 行 /var/www/jemstuff.com/htdocs/ero/functions.php 中的非法偏移类型

...我不知道为什么会收到该错误。第 76 行是:

if ( $woe['WoEDayTimes'][$i][0] == $curDayWeek && $woe['WoEDayTimes'][$i][2] == $curDayWeek ) { // Check the day of the week.

在functions.php中

var_dump($woe)

array(2) { ["WhoOnline"]=> string(2) "no" ["WoEDayTimes"]=> array(2) { [0]=> array(4) { [0]=> int(6) [1]=> string(5) "18:00" [2]=> int(6) [3]=> string(5) "19:00" } [1]=> array(4) { [0]=> int(3) [1]=> string(5) "14:00" [2]=> int(3) [3]=> string(5) "15:00" } } }

感谢您提供给我的任何帮助。:)

4

1 回答 1

2

几个小点:

  • foreach循环和for循环都可以正常工作,但您可能会发现更foreach方便,因为您不必count()检查几天/时间。

  • 您应该返回布尔值truefalse而不是 1 或 0。

我不确定您为什么会收到该错误,但我看到的更大问题是您如何比较时间。您将字符串时间转换为数字类型,并且不会像您想象的那样完全转换。例如...

"14:00" < "14:59"

...将是错误的,因为它将两个字符串都转换为 14。因此,第一个字符串实际上等于第二个。

您最好将字符串转换为 Unix 时间戳(即自 1970 年 1 月 1 日以来的秒数),然后进行比较。

这是我将如何做到的一个粗略的想法:

// Function to help get a timestamp, when only given a day and a time
// $today is the current integer day
// $str should be 'last <day>', 'next <day>', or 'today'
// $time should be a time in the form of hh:mm
function specialStrtotime($today, $day, $time) {

    // An array to turn integer days into textual days
    static $days = array(
        1 => 'Monday',
        2 => 'Tuesday',
        3 => 'Wednesday',
        4 => 'Thursday',
        5 => 'Friday',
        6 => 'Saturday',
        7 => 'Sunday'
    );

    // Determine if the day (this week) is in the past, future, or today
    if ($day < $today) {
        $str = 'last ' . $days[$day];
    } else if ($day > $today) {
        $str = 'next ' . $days[$day];
    } else {
        $str = 'today';
    }

    // Get the day, at 00:00
    $r = strtotime($str);

    // Add the amount of seconds the time represents
    $time = explode(':', $time);
    $r += ($time[0] * 3600) + ($time[1] * 60);

    // Return the timestamp
    return $;
}

// Your function, modified
function determineWoE($timeNow, $woe) {
    $dayNow = (int) date('N', $timeNow);
    foreach ($woe as $a) {
        // Determine current day

        // Determine the first timestamp
        $timeFirst = specialStrtotime($dayNow, $a[0], $a[1]);

        // Determine the second timestamp
        $timeSecond = specialStrtotime($dayNow, $a[2], $a[3]);

        // See if current time is within the two timestamps
        if ($timeNow > $timeFirst && $timeNow < $timeSecond) {
            return true;
        }
    }
    return false;
}

// Example of usage
$timeNow = time();
if (determineWoE($timeNow, $woe['WoEDayTimes'])) {
    echo 'Yes!';
} else {
    echo 'No!';
}

祝你好运!

于 2012-06-03T01:29:31.053 回答