2016-02-12 69 views
0

我想找到的逻辑,如果当前时间与此时间范围内躺在那么我的店是开放的,否则它是关闭 我是能够弄清楚的解决方案,但逻辑是不正确的,当我考虑分钟 唯一小时逻辑是如何检查当前时间是在开店和关闭时间之间的店铺

$time= "11:00 22:30"; //time interval 
$var=explode(" ",$time); 
$currenttime=date("22:00"); // currenttime 
$opentime =$var[0]; 
$closetime=$var[1]; 

if($opentime - $currenttime < 0 && $currenttime - $closetime < 0)  
{ 
    echo "open"; 
} 
else 
{ 
echo "close"; 
} 

此代码的工作很好,当我把当前时间作为10,24,9,但是当我考虑为22.30或11.30没有给出正确的结果,请为此建议我正确的解决方案..在此先感谢

+2

你觉得'日期( “22:0”)'是好? – deceze

+0

我们需要检查的当前时间 – techandroid07

+0

是的,但是......日期(“22:00”)返回......“22:00”......这是......没有意义的。 – deceze

回答

0

我建议t使用Unix时间戳。下面的scetch应该指向正确的方向。

$unixStart = mktime(11, 0, 0, date('n'), date('j')); 
$unixEnd = mktime(22, 30, 0, date('n'), date('j')); 

$unixNow = time(); 
if (($unixNow >= $unixStart) && ($unixNow <= $unixEnd)) 
{ 
    echo "open"; 
} 
else 
{ 
    echo "closed"; 
} 

请确保您所说的“date_default_timezone_set”和/或使用日期时间/ DateTimeZone类以获得正确的时区。下面
的示例使用 “美国/芝加哥”,设置所需的时区,而不是

$objDTZ = timezone_open("America/Chicago"); 
$objDate = new DateTime('now'); 
$unixNow=time(); 
$objDate->setTimestamp($unixNow); 
$objDate->setTimezone($objDTZ); 

然后

$unixStart = mktime(11,0,0,$objDate->format('n'),$objDate->format('j')); 
$unixEnd = mktime(22,30,0,$objDate->format('n'),$objDate->format('j')); 

谢谢
汤姆

+0

Mockrat dekuje,Pane Martin! –

0

我觉得像的爆炸字符串在这里有点不必要。 这是一个解决方案。

<?php 

$openingTime = '1100'; 
$closingTime = '2230'; 
$currentTime = date("Hi"); // Current time 

if($currentTime < $openingTime || $currentTime > $closingTime) { 
    echo 'Shop is closed.'; 
} else { 
    echo 'Shop is open.'; 
} 

此外,不要忘记使用date_default_timezone_set()设置默认值。 了解更多关于它here

0

可爱的一行;-)

echo (date("Hi") > 1100 && date("Hi") < 2230) ? 'Open' : 'Closed';  
+0

$ opentime = strtotime(“11:00”); $ closetime = strtotime(“22:00”); $ currenttime = strtotime(date(“H:i”)); ($ opentime - $ currenttime <0 && $ currenttime - $ closetime <0) {$ shop =“open”;} else {$ shop =“close”; }这个解决方案对我的支持感谢 – techandroid07