What you want is a CronJob:
What are CRON JOBS and how to use them with PHP
This example uses the cron mechanism of unix / linux, which is the most common to use, but there are mechanisms that integrate directly the language, as is the case of Quartz for Java, but I do not know any of that for PHP.
My tip: Use linux cron:)
Complementing the answer:
You need to add an entry in your system user's cron (linux):
Step 1: Create the file to 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 it 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 you're done!
To run exactly at the end of the month is a bit trickier, as the months have different end days (it may end on the 28th, 29th, 30th or 31st) the cron expression gets a bit more complicated.
The easiest way I found was by creating more than one entry in cron to cover the different cases.
See:
p>
link