在过去的几天里,我一直在断断续续地研究这段代码,但无法弄清楚。
我需要做的是从函数返回 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
),
);
但是,无论我做什么...函数确定WoE始终返回0。
我是否需要函数中的 foreach 而不是 for?如果时间在用户可设置的时间内,如何让 DetermineWoE 返回 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.
在函数中.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" } } }
感谢您为我提供的任何帮助。 :)
几个小点:
-
foreach
循环和for
循环都可以正常工作,但您可能会发现foreach
更合适,因为您不必count()
要检查的日期/时间。 -
应返回布尔值
true
或false
,而不是 1 或 0。
我不确定你为什么会出现这个错误,但我看到的更大的问题是你如何比较时间。您将字符串时间转换为数字类型,这不会像您想象的那样完全转换。例如。。。
"14:00" < "14:59"
。将是假的,因为它将两个字符串都转换为 14。因此,第一个字符串实际上等于第二个字符串。
您最好将字符串转换为 Unix 时间戳(这是自 1/1/1970 以来的秒数),并进行比较。
以下是我将如何做到这一点的粗略想法:
// 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!';
}
祝你好运!