How to stop or timeout a consumer from consuming after specific time in RabbitMQ using php

Here is a hello world example on rabbitmq docs about consumer using php and php-amqplib package:

<?php

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLibConnectionAMQPStreamConnection;

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

echo " [*] Waiting for messages. To exit press CTRL+Cn";

$callback = function ($msg) {
    echo ' [x] Received ', $msg->getBody(), "n";
};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

try {
    $channel->consume();
} catch (Throwable $exception) {
    echo $exception->getMessage();
}

$channel->close();
$connection->close();

Are there any ways to stop or timeout the consumer after a specific time?
And I wonder how the process can reach the close() method of connection and channel in the code above if there are not any exception occur because the consume() method is blocking it?

I found out that there is a basic_cancel method but I don know where and when to use it because like I said, the consume() method is blocking the code.