函数确定当前日期时间是否在PHP中用户设置的日期和时间内
在过去的几天里,我一直在断断续续地研究这段代码,但一直没有弄明白 我需要做的是根据当前时间是否在用户设置的时间内,从函数返回0或1。如果时间和日期在用户设置的4值数组内,则返回1,否则返回0。用户可以为多个时段设置多个阵列 我尝试使用此代码已有一段时间了: functions.php:函数确定当前日期时间是否在PHP中用户设置的日期和时间内,php,function,datetime,foreach,Php,Function,Datetime,Foreach,在过去的几天里,我一直在断断续续地研究这段代码,但一直没有弄明白 我需要做的是根据当前时间是否在用户设置的时间内,从函数返回0或1。如果时间和日期在用户设置的4值数组内,则返回1,否则返回0。用户可以为多个时段设置多个阵列 我尝试使用此代码已有一段时间了: functions.php: function determineWoE($woe) { $curDayWeek = date('N'); $curTime = date('H:i'); $amountWoE = count($w
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;
}
}
}
但是,不管我做什么…函数determineWe总是返回0
我是否需要函数中的foreach而不是for?如果时间在用户可设置的时间内,如何确定返回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)
谢谢你能为我提供的任何帮助。:) 几个小要点:
循环和foreach
循环都可以正常工作,但是您可能会发现for
更方便,因为您不必foreach
检查天数/时间count()
- 您应该返回布尔值
或true
,而不是1或0false
"14:00" < "14:59"
“14:00”<“14:59”
…将为false,因为它将两个字符串都强制转换为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!';
}
//当只给定一天和一个时间时,用于帮助获取时间戳的函数
//$today是当前的整数日
//$str应为“最后”、“下一个”或“今天”
//$time应为hh:mm形式的时间
函数SpecialStrotTime($today、$day、$time){
//将整数天转换为文本天的数组
静态$days=数组(
1=>“星期一”,
2=>“星期二”,
3=>“星期三”,
4=>“星期四”,
5=>“星期五”,
6=>“星期六”,
7=>“星期日”
);
//确定这一天(本周)是过去、未来还是今天
如果($day<$day){
$str='last'。$days[$day];
}如果有其他情况($day>$day){
$str=‘下一个’。$days[$day];
}否则{
$str=‘今天’;
}
//得到一天,在00:00
$r=STROTIME($str);
//添加时间所代表的秒数
$time=explode(“:”,$time);
$r+=($time[0]*3600)+($time[1]*60);
//返回时间戳
返回美元;
}
//你的功能被修改了
函数determineWoE($timeNow,$woe){
$dayNow=(int)日期($N',$timeNow);
foreach($a){
//确定当前日期
//确定第一个时间戳
$timeFirst=specialstrottime($dayNow,$a[0],$a[1]);
//确定第二个时间戳
$timeSecond=specialstrottime($dayNow,$a[2],$a[3]);
//查看当前时间是否在两个时间戳内
如果($timeNow>$timeFirst&$timeNow<$timeSecond){
返回true;
}
}
返回false;
}
//用法示例
$timeNow=time();
如果(determineWoE($timeNow,$woe['WoEDayTimes'])){
回声‘是的!’;
}否则{
回音‘不!’;
}
祝你好运 很接近…我可以找出如何获得时间范围,我认为这就是数组给我带来的问题。Array和我相处不好;)什么是输出:
var_dump($woe)代码>变量转储($woe)=数组(2){[“WhoOnline”]=>string(2)“no”[“woodaytimes”]=>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”[3]=>string(5][3]=>15:00注释不在转储位置。如果问题需要进一步澄清,请将此添加到问题中。对不起!编辑原始问题。嗨,谢谢!我发现的一个问题是您的SpecialStrotTime函数正在调用未引用的变量$a…我看到您在另一个函数中引用了它…我不确定您正在做什么或如何做您正在做的事情,所以我无法修复它。如果你能提供解释,谢谢你的解释!别担心!我的错误,$a[0]应该是$day。我编辑了答案以反映它。谢谢!太完美了!很高兴听到!祝你的项目顺利完成!
"14:00" < "14:59"
// 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!';
}