Mark Alan
Mark Alan

Reputation: 455

Show list at particular timings

I have 2 lists stored into the database

1:First list is 3pm that have a stored time as 03:00:00 PM which will end at 12:00:00 AM and the

2:Second list is 11pm list which time is 11:00:00 AM and will be active till 3pm so the scenario is from 12am to 3pm

I want to show 11am list and from 3 pm to 12 am I want to show the 3om list but right now it's only showing 3 pm list as it's 7:00 am here can anyone help me out with this below is my code which I have used

if(date('h:i:s') >= '12:00:00 AM' && date('h:i:s') < '03:00:00 PM') {
    $query   = mysqli_query($connection, "SELECT * FROM list WHERE status = 'Active' AND time_list = '11:00:00 AM'");
}
if(date('h:i:s A') > '03:00:00 PM' && date('h:i:s A') < '12:00:00 AM') {
    $query   = mysqli_query($connection, "SELECT * FROM list WHERE status = 'Active' AND time_list = '03:00:00 PM'");
}
$dt = mysqli_fetch_array($query);

Please help me out with building up this scenario

Upvotes: 0

Views: 30

Answers (1)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try to use strtotime() and use time range 3 PM to 11:59:59 instead of 12:00:00 otherwise it will check time for same day 12 AM which is not the case.

$nowH = strtotime(date('H:i:s')) ;
$sql="SELECT * FROM list WHERE status = 'Active' ";
if($nowH >= strtotime('12:00:00 AM') && $nowH < strtotime('03:00:00 PM')) {
    $sql .= " AND time_list = '11:00:00 AM'";
}

if($nowH > strtotime('03:00:00 PM') && $nowH <= strtotime('11:59:59 PM')) {
    $sql .= " AND time_list = '03:00:00 PM'";
}
if($sql){
   $query = mysqli_query($connection, $sql);
}

Online Demo

Upvotes: 1

Related Questions