我想根据两个时间戳之间的时间范围显示消息

我想根据两个时间戳之间的时间范围显示消息

问题描述:

My goal to display a message if time range is between given range If not display another one. I tried;

date_default_timezone_set("Europe/Istanbul"); 
$saat = date("h:i");
if ($saat <='08:00' && $saat >='22:00') {
    echo ('yes we are open');
}
    else ('sorry we are closed');{
}

I know i make mistake while trying to get that if time is between the range, but i cannot overcome problem. waiting for your responses.

我的目标是在时间范围介于给定范围之间时显示消息如果不显示另一个。 我试过;

  date_default_timezone_set( “欧洲/伊斯坦布尔”);  
 $ saat = date(“h:i”); 
if($ saat&lt; ='08:00'&amp;&amp; $ saat&gt; = '22:00'){
 echo('是的,我们是 打开'); 
} 
其他('抱歉,我们已关闭'); {
} 
  code>  pre> 
 
 

我知道我在尝试获取时犯了错误 如果时间介于范围之间,但我无法克服问题。 等待您的回复。 p> div>

Try the following.

    $saat = new DateTime(); 
    $open = new DateTime( $saat->format('Y-m-d').' 08:00',new DateTimeZone('Europe/Istanbul'));

  $close = new DateTime($saat->format('Y-m-d').' 22:00',new DateTimeZone('Europe/Istanbul'));

    if (($saat >= $open) && ($saat <= $close)) {
        echo 'yes we are open';
    }else{
        echo 'sorry we are closed';
    }

Try this

date_default_timezone_set("Europe/Istanbul"); 
$saat = date("h:i");
if ($saat <='08:00' && $saat >='22:00') 
{
    echo 'yes we are open';
}
else 
{
    echo 'sorry we are closed';
}

Its better to perform a less than / greater than operation on a DateTime object. Also I think you have your >= and <= confused, and you have an extra bracket.

I changed the name of the $saat variable to $now to make it more understandable.

date_default_timezone_set("Europe/Istanbul"); 

//Create a DateTime Object represent the date and time right now
$now = new DateTime(); 

//Today's Opening Date and Time
$open  = new DateTime( $now->format('Y-m-d').' 08:00' ); 

//Today's Closing Date and Time
$close = new DateTime( $now->format('Y-m-d').' 22:00' ); 

if ($now >= $open && $now <= $close) {
    echo ('yes we are open');
}
    else ('sorry we are closed');{
}

On a side note, I NEVER use date() because of the 2038 problem (google it). DateTime is not subject to such problems.

If you don't care about the minutes you can do it like this

date_default_timezone_set("Europe/Istanbul"); 
$saat = date("h");
if ($saat<=8 && $saat>=22) {
  echo ('yes we are open');
} else { 
  echo('sorry we are closed');
}

you can use your condition like

if (strtotime($saat) <=strtotime('08:00') && strtotime($saat) >=strtotime('22:00'))

Ok here is the code :

date_default_timezone_set("Asia/Karachi"); 
$t=time();
//echo(date("H:i",$t)); // Current Time
$hour = (date("H",$t)); // Current Hour
$minute = (date("i",$t)); //Current Minute


if (($hour <= 8)&&($hour >= 22)) {
    echo "We are open";
}
else {
    echo "sorry we are closed";
}