orko
orko

Reputation: 127

I would like to show messages according to time range between two time stamps

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.

Upvotes: 0

Views: 840

Answers (6)

Mubo
Mubo

Reputation: 1070

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';
    }

Upvotes: 2

Syed ObaidUllah Naqvi
Syed ObaidUllah Naqvi

Reputation: 474

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";
}

Upvotes: 0

Tushar Gupta
Tushar Gupta

Reputation: 15913

you can use your condition like

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

Upvotes: 0

Dutchie432
Dutchie432

Reputation: 29160

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.

Upvotes: 1

gdros
gdros

Reputation: 393

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');
}

Upvotes: 0

Seegan See
Seegan See

Reputation: 101

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';
}

Upvotes: 2

Related Questions