Ad has started or has no start time (runs immediately) // 2. AND // 3. (end_date IS NULL OR end_date >= NOW()) -> Ad has not ended or has no end time (runs forever) $sql = "SELECT * FROM horizontal_ads WHERE (start_date IS NULL OR start_date <= NOW()) AND (end_date IS NULL OR end_date >= NOW()) ORDER BY RAND() LIMIT 3"; $stmt = $conn->query($sql); if ($stmt) { $horizontal_ads = $stmt->fetchAll(PDO::FETCH_ASSOC); } } } catch (Exception $e) { // Ideally log this to a file instead of showing user, but keeping simple for now error_log("Error fetching horizontal ads: " . $e->getMessage()); } $ad_count = count($horizontal_ads); ?> 0): ?>