Send Cron Job Notification By Timezone Php
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?
Answer
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.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?