Php 如何允许用户仅在工作时间登录?
我想制作一个限制用户在工作时间后登录的代码: 用户只能在8:00到4:35之间登录: 下面是我尝试过的代码,我设法使两个条件起作用,但第三个条件不起作用:任何帮助都将非常感谢:Php 如何允许用户仅在工作时间登录?,php,mysql,Php,Mysql,我想制作一个限制用户在工作时间后登录的代码: 用户只能在8:00到4:35之间登录: 下面是我尝试过的代码,我设法使两个条件起作用,但第三个条件不起作用:任何帮助都将非常感谢: <?php # function to get right time of the targeted city date_default_timezone_set('Africa/Johannesburg'); function time_diff_conv($start, $s)
<?php
# function to get right time of the targeted city
date_default_timezone_set('Africa/Johannesburg');
function time_diff_conv($start, $s)
{
$string="";
$t = array( //suffixes
'd' => 86400,
'h' => 3600,
'm' => 60,
);
$s = abs($s - $start);
foreach($t as $key => &$val) {
$$key = floor($s/$val);
$s -= ($$key*$val);
$string .= ($$key==0) ? '' : $$key . "$key ";
}
return $string . $s. 's';
}
$date = date('h:i');
echo $date;
/*Start time for work*/
$workStart = strtotime("08:00");//am
/*Stop time for work*/
$workStop = strtotime("04:35");//pm
/*This is the current time*/
$currentTime = strtotime($date);
//$fromSatrtToEnd = $workStart->diff($workStop);
/*This Condition works*/
if($currentTime >=$workStart){
echo "Start Working";
/*This Condition also works*/
} else if($currentTime < $workStart){
echo "You too early at work";
}
/*This Condition does not works*/
else if($currentTime < $workStop){
echo "Its after work";
}
?>
如果($currentTime>=$workStart和$currentTime必须明确日期的时间段,则程序无法识别08:00是上午日期,04:35是下午日期
只需在中更改变量声明
/*Start time for work*/
$workStart = strtotime("08:00 AM");//am
/*Stop time for work*/
$workStop = strtotime("04:35 PM");//pm
或将第二个变量转换为24小时格式:
$workStop=strotime(“下午16:35”);//下午
这样,与$workStop
相比,$workStart
将被正确评估为次要值
然后更改if以正确匹配条件:
if($currentTime >=$workStart && $current <= $workstop){
echo "Start Working";
} else if($currentTime < $workStart){
echo "You too early at work";
}
else if($currentTime > $workStop){
echo "Its after work";
}
如果($currentTime>=$workStart&&$current$workStop){
回应“下班后”;
}
首先,如果您必须管理上午/下午的小时数,您应该在使用小时数的每一行中将其转换为24小时格式:
如果你的条件不合适,你应该有如下的条件:
if($currentTime >=$workStart AND $current <= $workstop){ // need to check both
echo "Start Working";
} else if($currentTime < $workStart){
echo "You too early at work";
}
else if($currentTime > $workStop){ // > rather than <
echo "Its after work";
}
如果($currentTime>=$workStart和$current$workStop){/>而不是<
回应“下班后”;
}
当我把这个和$currentTime放在一起时,非常感谢我将Kawashita86的想法与你的代码结合起来,然后它起了作用,我真的很感激我希望我能胜过你:你做得很好,现在我的代码起作用了,这是为了与腰肉代码集成
$date = date('H:i'); // H rather than h
$workStart = strtotime("08:00");
$workStop = strtotime("16:35");
if($currentTime >=$workStart AND $current <= $workstop){ // need to check both
echo "Start Working";
} else if($currentTime < $workStart){
echo "You too early at work";
}
else if($currentTime > $workStop){ // > rather than <
echo "Its after work";
}