1) As far as implementing functions goes, It is pretty simple. If you need to use your function in multiple PHP documents, place int in a separate file and use an include to bring it into the document(that the client calls):
<?php include_once($_SERVER['DOCUMENT_ROOT']."/Path/To/file/phpscript.php");
If you only need it in one document, simple place it in in the document(that the client calls) before you use it.
2 & 3) There are "cron jobs" if you'r using Linux(I personally don't like using them), which could be used to awaken the php script every 5 minuets(in which case keep the function in its own file), but instead I would (edit: not, use cron jobs, this is a last resort)recommend another method instead. In your function(which will run when the client requests it), write the time you last requested it in to some text file, database or anything else. when the function runs, check if the written time was more than 5 minuets ago. If it was, create a new number and write a new time down. If it wasn't, fetch the old number from the database.
EDIT: Use Cron Jobs