What you want is a Cronjob:
What are CRON JOBS and how to use them with PHP
This example uses the Unix/linux cron mechanism, which is the most common to use, but there are mechanisms that integrate directly into the language, as is the case of Quartz for Java, but I don’t know any of this for PHP.
My tip: Use linux cron :)
Complementing the answer:
You need to add a cron entry to your system user (linux):
Step 1: Create the file that will be executed:
helloworld.php file
<?php
echo 'Hello world';
?>
Step 2: Start the change in the system cron file using the 'crontab -e' command.
This command will open the cron file using the default editor, probably vim
Step 3: Add the desired cron entry with the file that was created. In this example I will put to run every first day of the month at 9 am:
0 9 1 * * php -f /home/murilo/meu_php/helloworld.php > /dev/null 2>&1
Description:
0 9 1 * * -> Expressão cron para executar em todo primeiro dia do mes as 9 da manhã (para mais exemplos acesse https://crontab-generator.org/)
php -f /home/murilo/meu_php/helloworld.php -> Comando a ser executado
> /dev/null 2>&1 -> Direcionando sysouts para /dev/null
Step 4: Save the file and ready, it is configured !
To perform exactly at the end of the month is a little more complicated, as the months have different end days (can end on 28, 29, 30 or 31) the expression cron gets a little more complicated.
The easiest way I found was to create more than one cron entry to cover the different cases.
Behold:
https://stackoverflow.com/questions/6139189/cron-job-to-run-on-the-last-day-of-the-month
https://blog.nexcess.net/2013/05/09/cron-job-for-the-last-day-of-the-month/
Possible duplicate of How to create a Cron job?
– Roberto de Campos
Possible duplicate of Alert by email such a day
– Pedro Augusto