Setup reminder system with cron - php

I have specific dates for users interviews. Here is my table:
interviews
-------------
user_id
interview_date
interview_time
reminder_time
The reminder time can be 1 hour, 5 hours, 1 day, 3 days, 7 days, and 14 days.
How would I go about setting up a cron job or something to check the database and send out a reminder if the interview is coming up?
For example, if the interview is 5 hours away, the database would send an email reminder to that user.
Should I use cron? Daemon? Not familair with daemon, just saw it when I was searching it up. Or should I use something else? This is a pure/native PHP application with no frameworks.
Here is what i have so far:
$check = $db->prepare("SELECT * FROM interviews");
if(interview is coming up) {
sendEmail();
}
but i'm confused as to where and how should I search for these interviews and send out reminders.
If i search for ALL interviews, that is putting a huge load on the server.
thank you for all the help and suggestions!

Unless you want your PHP process to run perpetually you'll need some kind of regular scheduler like cron. This can set your code to run once every minute, every hour, or whatever interval makes sense.
Normally every five or fifteen minutes is sufficient to check for upcoming events and send out notifications for them.
Be sure to set a flag in your database for if something has been notified already so if your script crashes and you need to re-run it can pick up where it left off.

Related

Run php calculations when the browser is closed

I'm trying to create a YouTube-like service where the content creator is paid for their content however I'm a bit stuck.
The website needs to calculate the amount of views a content creator gets a month to calculate how much they need to be paid and then through PayPal API pay them by the end of the month. The problem is that this needs to be done even when the page is closed. I assume this is done on a server but I not sure if that's the case or even how to do it.
Thanks
Just googling real quick on cron, I found this website: https://www.pantz.org/software/cron/croninfo.html
I have used cron a lot before and the information here seems adequate to answer your question; it at least will arm you with the knowledge needed to do what is required.
An example for a cron job could be the following:
50 * * * * /myTask.php
What this says is "run this script 'myTask.php' every 50 minutes, every hour, every day of the month, every month, every day of the week" - simply put, run every 50 minutes.
The aforementioned task can be added to the crontabs file by using the following command from the terminal crontab -e
I found the solution. It turns out that the service hosting my website has an option to run cron jobs internally so I can make a script to loop through all the users, calculate their view earnings and pay them and run it at the beginning or each month.

Execute a timed command in a time command

I am working with an php application where i need to check a database of users for whom has birthday today.
This i do through a cron job.
Now, when i get the users with birthday, i need to send them a SMS.
The SMS has an individual time associated which determins when the SMS should be send.
Lets say John and Peter has birthday today. Peter works from 01-09 so he should get the SMS at 01 and Peter works from 08-16 so he should get his SMS at 08.
I was thinking about dynamically creating the same amount of cron jbos which equals the people who has birthday on a given date. Those cron jobs would then send the SMS to the appropiate people this one time and thats it.
My question is, is there a smarter way to do this?
Secondly, if the cron job generation idea is good enough, is there a way to remove individual old cron jobs so i dont clug up the cron job list with old jobs which will never be run again?
Thanks in advance.
If you would like to send messages to each person at a specific time using cronjobs then you would have to create a cronjob for each person. Very bad idea in my eyes as you could end up with over 1000 cronjobs!
You would be much better with a system that runs a cronjob every 5 minutes or so (however often you want), and this cronjob executes a PHP script to determine if there are any messages to send out at that time, or between now and the last cronjob.
Whilst this may mean your users could get their message up to 5 minutes late, it is unlikely because nobody I know starts work at 8:02, and therefore, if the cronjob is at 8:00, 8:05, 8:10 etc, you will almost always get the message sent right on time!
If you're worried about creating too many cron jobs (not sure how many would be a problem on your system), then why not create just one cron job at the OS level? Then you can call your program every 10 minutes or so, and it can check if there are new people that need to be sent an SMS.

Schedule e-mail notification 24 hours before certain event?

I have a PHP-based site with bookings/appointments stored in a MySQL database.
I want to set an e-mail notification to be sent to each person who made a booking exactly 24 hours before their booking. Bookings are added through a regular PHP form.
I know I could add a script to, for instance, the index page that checks the database for any bookings that are in the final 24 hours, but that's unreliable since I won't be getting much traffic at first. So the index page could go hours without visits, and the notification would be hours late.
The other solution that came to mind is to set a cron job that runs every minute, calls a PHP script which checks whether any e-mails should be sent and sends them. But I'm not sure if this is overkill in a way; does anyone have a better solution than having something run in the background every minute?
To sum it up - is there a way to do this without cron?
Triggering the job from a web page is a very bad idea, for two reason: (1) if you don't get traffic to your site, the job doesn't run; (2) if you get a lot of notifications, the job will be slowing down the response to the web requests (assuming you invoke the job synchronously).
I would strongly discourage you from running a job every minute either - it definitely will be an overkill. Instead, think whether you really need "exactly 24 hours" as the interval or would "between 22 and 26 hours" be ok.
We have a similar requirements - and went about it by setting a job that runs every 4 hours and checks what notifications need to be sent for events starting between 22 and 26 hours form the time the script runs. This way, the script is only execute 6 times in a day and everything gets sent correctly.
If 4 hours approximation is not good enough, then think to the largest interval that's appropriate. I'm sure 1 hour should be sufficient. Have a script run once an hour (from cron) and check for events starting between 23 and 24 hours from the time of the run.
Remember that once your email is sent, it doesn't end up in the recipient's inbox immediately: sometimes it takes a few seconds, but sometimes it may take an hour or even more - so an extra hour difference in your script won't be a problem.
You don't need to use cron as an interval'd timer. You can set a very specific date and time when you want your job done.
Here's an article on the subject
For instance:
0 0 18 5 * <php command here>
Will run every May 18th at midnight. That's more than enough time to clear it before the next iteration (next year).
there is no way other than setting cron or sending request to server through periodic calls...below is post similar to your question, you may get idea.
Live redirect based on periodic server calls with JSON or AJAX
Thanks
a cron job every minute has no sense ! but you can do a cron job every hour because i think it doesn't meter a hour difference or 2 hours . without cron it isn't any other way . it will take about 2 second (at max) to complete so it is worth

Resent mysql values at certain time

I have a database with a table and some columns. One of these columns is flags (similar to SO) where users can flag comments. I would like to give each user 5 flags per day. So if a user uses 2 flags in a 24 hour period, the flags should reset to 5 at the end of the 24 hours. I really have no idea how to do this. Is there a special mysql function?
PHP:
$query=mysql_query("UPDATE users SET flags='5' WHERE userID='$user'");
how would i get this to repeat every 24 hours? (if this is the right solution)
The best way is probably to set up an automated task (possibly using cron) that runs a query to do this.
Make a script and add to cron job. It will automatically update all on specified time of day.
Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it’s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.
Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!
You can use MySQL Event Scheduler and define an event to let it update every interval you want.

Scheduled cron job to check for pending activity

Using PHP ...
This is for my personal use so I'm thinking maybe 3-4 emails a day.
I'm at a point where I can send an email to a dedicated email address where my script parses the message and stores it into a DB. Now, I need to figure out the best way to check the records in the DB for any upcoming task. I feel like I'm missing something, maybe like a trigger field as to when a reminder should go out. However, that's not a concern to me at the moment since I'll just send an alert 15 mins prior to the due date.
Question is, shoudl I run a cron job that queries the DB every minute? I take it the query will have to say something like "select all tasks that is due within 15 minutes."
A cron job seems reasonable. You just query based on the dueDate:
MySQL: SELECT * from jobs WHERE dueDate > NOW() + INTERVAL 15 MINUTES
SQL Server: SELECT * from jobs WHERE dueDate > DATEADD(GETDATE(),15,minute)
A quick cron job should be fine, for something small. It had better be a quick query though, or you'll get booted if it's a shared server.
Why don't you use those free cron services if on shared server? For something this simple, that is your best bet!

Categories