Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

concentrico's avatar

Cron running in multiple timezones

Hi! I'm having trouble figuring how to run con jobs or laravel tasks in different timezones. I'm building a SaaS app in which I have many clients from many different timezones. I need a cron to run during the last day of the month for each of the timezone's. Example: User in Tokyo User in Mexico City User in New York

Each one has different timezones and I need to run the cron for each of those timezones at 8:00 PM on the last day of the month. How do I create this? My main concern is as the user database grows, I don't know an efficient way to achieve this!

Any help is greatly appreciated.

0 likes
3 replies
nfauchelle's avatar
Level 11

@concentrico

Think about this problem first.

What happens on the last day of the month, at the time the server is going to run the cron something happens and the server is down for 30 minutes. When should the cron run now?

So lets solve that problem first.

We'd probably run a command, which "checks to see if today is the last day of the month for this client and it's past 8PM, and if we haven't successfully run the code we need to run, then run it".

So we could create cron log object (or, end of month report or end of month cleanup)

client_id | month | status

So we can then query this table to see if we have ran the cron already.

Then if we just set the cron to run every 5 minutes all the time, check to see if it's ran for the client (db query) and if not and it's the last day of the month and it's past 8PM for that client then log and go.

Make sensish?

That way if server is down for a bit, or something happens it will still run.

You could fake 1,000 users and see what performance is like. But if you have 1,000 users and they are paying even $1 then this becomes a nice problem to solve :)

--

TLDR; Track if the cron has run for the client for the month or not. If not and past the time needed then run it. Run the cron every 5 minutes, even if only on the 28th to the 2nd server time to catch all...

2 likes
Snapey's avatar

I would probably a) run the server in UTC b) create a queue of tasks to be run at a specific time (UTC) c) when adding new client in new timezone, convert 8PM on the last day in their timezone into UTC and use this in the list of jobs

So, the time that the report needs to run is converted to UTC at the point of scheduling it, not at the point of execution.

Then, at each cron interval, check the list of tasks to perform and see if any of them are overdue.

1 like
aniljaiswal's avatar

I am building a SaaS app myself and was looking for a solution for the exact same problem and I have to say I agree with @Snapey here since I don't need to run a cron every 5 minutes. And if the scheduled job fails or anything, I can just re-run it later. It's much easier to convert the task into the correct time zone when scheduling it.

Please or to participate in this conversation.