I need to execute a piece of code ONCE every first day of the month only. For simplicity, let’s say i want to echo a string. With the help of built-in PHP functions determining the first/last day of the month, and the current/next month is a no-brainer.
My initial plan of action is to store the current and next month in some variable. And once the first day of the new month hits, only then that echo should be executed.
My problem lies in, for one, I cannot find a way to actually store these variables (something like having them as constants) for me to able to compare them.
Why not just a cron job? The pattern for a monthly task would be:
There’s no need to persist values or perform date logic (especially if by “variables” you mean something within the application itself, which probably won’t/shouldn’t be running 24/7 if it’s not a service/daemon of some kind, like cron is), scheduling systems are designed specifically for what you need. (If you’re not on Unix/Linux/etc. then other systems have other schedulers. I’m just assuming Linux since that’s most common for PHP.)