How we can make an cronjob in php ???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

How we can make an cronjob in php ???

Post by djdon11 »

hi friends ..

i want to send email on every fifth day to my users ..

for this i have to run my php script page on every fifth day automatically & for this i think i need an cronjob which run my page automatically on every fifth day .. on server

anybody let me know how to make an cronjob & how we can set it on our server ..

i don't know anything about the cronjob .......

please help me it is urgent :cry:
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

You go into your control panel (of your webhosting account) and there should be an option for Cron Jobs somewhere there. Go there, and tell it the command (for PHP files, I'm not sure if you just type the name of the php file or if you have to type something else.. I think you have to type something else, google "php cron jobs" "cron jobs php script" or something) and you tell it how often to do it (every 5 days).
djdon11
Forum Commoner
Posts: 90
Joined: Wed Jun 20, 2007 5:03 pm

if there is any othere way to mail instead of set a cronjob?

Post by djdon11 »

hi..

Thanks for your reply ..

i want to know if there is any other way to for sending emails automatically on every fifth day .. instead of set an creonjob on server ?

if there is any idea then please let me know ... i need it urgently ...

Thanks ....
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

If you did it another way, you'd be reinventing the wheel. Why not cron?

Also, we typically stone people who double post...
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Well, there is one other way. If you have high traffic to your website, this will work very well, but if you don't, it might not.

What you do is have an integer in your database, in a table, which keeps track of the time that you sent your last e-mail. It should be in timestamp form (the code date("U") will give you the current time in timestamp form). Then, in your PHP code that is executed whenever someone visits your website, you check the last time that you sent an e-mail (with that integer in your database) and see if it was at least five days ago. If true, you send out an e-mail and update the time.

Note that when updating the time, it would usually be best to add five days (in seconds) to that value rather than just update it with the current time. Just make sure that the value you start with (before you ever run the script) is recent, and not 0 or something (because if you're adding to the current time, and you start with 0, it would be sending years' worth of e-mails-- so have the value start at the timestamp of today or yesterday or five days ago or something rather than 0).

Also, this method will only work if you have decent traffic to your site, because the check will only take place whenever someone visits your site. As long as you get one hit per day, though, it should be fine... though if you want your e-mails sent around the same time every day, you'd want a visitor every hour or whatever. A visitor every hour would mean that the e-mails would be sent around the same time (within the same hour) of every 5th day-- example: every 5th day somewhere between 1:30 and 2:30.
Post Reply