Reputation: 1246
I’ve written a PHP script to send emails. Now I’d like to add a feature to send emails according to a schedule. I am only aware of functionality that will execute a task triggered by a user action (for example, if a user presses a form button then the script evaluates whether the current time is greater than the scheduled time and executes the action accordingly).
Is there a way to set up an unmonitored system that executes actions according to a schedule?
Upvotes: 1
Views: 1085
Reputation: 73271
You should use a cronjob for this!
Example for a cronjob, running a php script at 6pm every day:
0 18 * * * php /path/to/script.php
Setup a cronjob via ssh:
1.Type:
crontab -e
Add the line to the file:
0 18 * * * php /path/to/script.php
Save the file
Done
If you don't have access to a linux machine/server, there are many free cron services available!
How to setup a cronjob in general:
# * * * * * command to execute
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
Special characters in cronjobs
Asterisk (*)
The asterisk indicates that the cron expression matches for all values of the field. E.g., using an asterisk in the 4th field (month) indicates every month.
Slash ( / )
Slashes describe increments of ranges. For example 3-59/15 in the 1st field (minutes) indicate the third minute of the hour and every 15 minutes thereafter. The form "*/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field.
Comma ( , )
Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) means Mondays, Wednesdays and Fridays.
Hyphen ( - )
Hyphens define ranges. For example, 2000-2010 indicates every year between 2000 and 2010 AD, inclusive.
Percent ( % )
Percent-signs (%) in the command, unless escaped with backslash (), are changed into newline characters, and all data after the first % are sent to the command as standard input.
Upvotes: 3
Reputation: 1293
You can create a Cron job to automate a PHP task.
Tutsplus - How to create a Cron Job
And as Rafael said, if you're using CPanel, this may help.
Upvotes: 0
Reputation: 805
PHP has nothing like this. PHP gets executed only when you enter praticular page. Then its sent to server, that runs the script and gives you the results.
You should consider CRON, it basically runs praticular script (pagexxx.php) in a set schedule.
Upvotes: 1
Reputation: 7746
You can configure your server to go to a page in which you write the PHP do do something.
The server will go to the page weekly, daily, hourly, yearly...however you specify it.
Upvotes: 0