Alkir this is obviously related to https://discuss.flarum.org/d/19707-last-modified-flarum , maybe it would have been better to not split the discussion. But now it's going in a different direction if you ask how to insert this precise code in Flarum.
The answer to that question is that while it might be possible, it's probably not a good idea. Flarum is built with frameworks to handle requests and response without sending anything directly down the pipe, and only when the request is finished handling, headers and content are written back in the HTTP response.
To achieve this you'd need a middleware, then use the PSR classes for the response to alter the headers of the response object.
Some good example middlewares to get you started are
https://github.com/flagrow/terms/blob/master/src/Middlewares/RegisterMiddleware.php
https://github.com/migratetoflarum/canonical/blob/master/src/Middlewares/CanonicalRedirectMiddleware.php