Issue
I want to write a script that will send push notification to users' phone every day at 2 pm. But there are users from all over the world, so I need to send that notification at time depending upon the timezone of particular user, which I keep in DB like this 'GMT+4'.
I need to do this by Cron job. I have never used that, so I don't know how to write that script and make this work in PHP. Anybody can help me?
Solution
A very simple approach is to have a cron-job call a PHP script every 1 minute, and have it check the database for things that need to be done. If something needs done now(), then do it.
In crontab you can use something like this,
* * * * * /usr/bin/php /path/to/my/script.php
And script.php can be like this
<?php
$ts = time();
// Run for up to 50 seconds
while($ts + 50 > time())
{
... SELECT id, time FROM Table WHERE time <= timeFromGMT(GMTvalue) ...
if(returns a row){
Send notification;
Set a flag that notification is send;
}
sleep(5);
}
Note: Use some locking mechanisms to prevent multiple processes at the same time.
Answered By - Abey Answer Checked By - Timothy Miller (WPSolving Admin)