awnew awnew - 27 days ago 7
PHP Question

Conditional tag with DateInterval

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

Answer

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