0

how to use conditional tags using DateInterval with hour for scale in PHP

for example i will add text "active" if at 21:00 until 23:00

<?php 
  $Hr = date('H:i'); 
  if($hr == 21:00 - 23:00) { 
    echo "active"; 
  }
  else { 
    echo "not active"; 
  } 
jitendrapurohit
  • 9,435
  • 2
  • 28
  • 39
awnew
  • 3
  • 2

2 Answers2

0
$Hr = date('H:i'); 

  if(strtotime($hr)>strtotime(21:00) && strtotime($hr)<strtotime(23:00) ){
    echo "active";
  }else{
     echo "not active"; 
  }

Please try this this may work.

Pranav MS
  • 2,235
  • 2
  • 23
  • 50
0

Try this, use DateTime to compare time.

$time1 = new DateTime('21:00');
$time2 = new DateTime('23:00');
$interval = $time1->diff($time2);
//echo $interval;
$diff =  $interval->format('%H').":".$interval->format('%I');
$Hr = date('H:i'); 
echo $diff." == ".$Hr."\n";

if($hr == $diff) { 
    echo "active"; 
}
else { 
   echo "not active"; 
}

DEMO

Dave
  • 3,073
  • 7
  • 20
  • 33