在过去的几天里,我一直在断断续续地处理这段代码,但无法弄清楚。
我需要做的是从函数返回 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" } } }
感谢您提供给我的任何帮助。:)