There is a cron function, details here:
https://codex.wordpress.org/Function_Reference/wp_cron
Be aware that it is not as precise a service as you may expect, I think its resolution is related to the frequency of page loads.
Not so sure about your second issue, maybe you have to put some code in your child-themes header function, or a page template. Do an SQL query on your custom table, selecting rows with timestamp after NOW - 12 hours, if it succeeds then there is nothing to do, if it does not then log the ID and add a new row.