How to create a cron PHP function every day? [duplicate] - php

This question already has answers here:
How to create cron job using PHP?
(11 answers)
Closed 7 years ago.
I need to create a simple automatic daily function to set my database columns to 0.
I know how to do with UPDATE using mysql query.
I need to know how set an automatic function without using refresh, Java or other. I don't want to refresh page every xx seconds/min/h. I need this function to start at 8 am.
This because i need to change all my customers column and set to 0 daily access.
I'm using php, html and a mysql database. Same story with a 1ST day month cron. I need to reset all to 0 every 1st day of each month (automatically).
I want to use this cron also for activate my web page only from 10 am to 3pm, don't ask me why, i'm doing examples.
I already read other topic but i don't know how write code for cron, i m a beginner. I found this: 00 08 * * 1 php --q directory/cron.php Maybe it runs every MONDAY something.... right... but how can i set my function to start all every day using *** ok but please explain me how to run functions and where i should write them.

Don't use a cron if you do not require any data processing or function handling. If you are simply updating your database on a schedule, use MySQL events.
Here is the documentation on ensuring that MySQL event scheduling is on:
19.4.2 Event Scheduler Configuration
Here is the documentation on creating events: 13.1.11 CREATE EVENT Syntax
Example of what you're asking it to do daily at 8am:
CREATE EVENT do_things
ON SCHEDULE
EVERY 1 DAY
STARTS (TIMESTAMP(CURRENT_DATE) + INTERVAL 1 DAY + INTERVAL 8 HOUR)
DO
UPDATE *update query to make your reset happen goes here*
You can use this as a start and modify it as needed based on the information in the CREATE EVENTS documentation.

Related

Hi, How to auto execute a php action page on every day at given time, i.e i want to execute a php script at 10:00 am everyday (In windows).? [duplicate]

This question already has answers here:
How to run a PHP file in a scheduled task (Windows Task Scheduler)
(7 answers)
Closed 5 years ago.
How to auto execute a php action page on every day at given time, i.e i want to execute a php script at 10:00 am everyday (In windows).?
I want to take the given time from the database and the n i want to execute the php script at the specified time taken from the DB
You will have to use cronjob if you want to execute specific php script, but if you just want to run a mysql 'job', you could use mysql event feature, which allows you to run mysql jobs at scheduled times.
Discussion which one to use: Cronjob or MySQL event?
Here's the manual: https://dev.mysql.com/doc/refman/5.7/en/events-overview.html
And here is a simple example: https://www.sitepoint.com/how-to-create-mysql-events/
On windows 7 you could setup a Task Scheduler task:
Have Administrator rights
Start->Control Panel->System and Security->Administrative Tools->Task Scheduler
Action->Create Basic Task->Type a name and Click Next
Finish the wizard as you like

PHP: Auto update a database field on a certain date, no scripts via cronjob

Not sure if it's even possible but is there a way to auto update a database field on a certain date?
I've just been given a system to work on and there is zero documentation on the processes and no comments within the code and more than 2000 files with thousands of lines of poorly written code. Btw it's a custom MVC system.
Now the current system automatically updates a database field 24 hours in advance before the date arrives. There are no cronjobs running. Could it be possible to auto update a field when a certain date occurs? Or is the code being triggered somehow? Can this be done via a PHP script?
Basically the system expires items 24 hours before their expiry date arrives.
Already been through the following material but no help so far:
PHP and MYSQL date expiry near
get a time difference between two dates
PHP MySQL - Select all where expiry date = todays date + 7 days
http://www.interactivetools.com/forum/forum-posts.php?postNum=2215219
http://www.dreamincode.net/forums/topic/317765-how-to-set-expiration-date-in-php/
I think you can draw attention at one more feature MySql - "Using the Event Scheduler"
https://dev.mysql.com/doc/refman/5.1/en/event-scheduler.html

Regularly performed script [duplicate]

This question already has answers here:
Can a PHP script be run regularly on a server without requests from a client?
(5 answers)
Closed 8 years ago.
I am wondering if the following can be accomplished with PHP. Lets say that you have simple scripts which only insert some records into database (lets say some reporting) e.g. money incomes/withdraws.
And every 13th day in a month, you have some regular payment for electricity, mortgage etc. Is it possible, that PHP on 13th day every month at 00:00:01 AM perform some insert action.
In addition, is it possible that this is performed without even logging/using the application (without user interaction)? Is there a way how ensure that such action will be definately performed only once a month?
Thank you
What you're looking for is a cron job.
They are independent of any programming language.
This can be accomplished using a cronjob. You can find more information about that here.

How to launch a php script 12 hours after each user triggered event

Any of the users can trigger an event, as many times they wish. Each time this event is triggered, a row in a mysql table is created with the timestamp, their userid and other useful information. What I need to do is launch a script which runs exactly 12 hours after the event is triggered i.e. for each row in the table.
How can I achieve this in an automated and efficient fashion?
You could use a cron job which every minute launches the script.
In the script you should first fetch the row(s) and check if it's OK to run (if 12 hours passed) then continue, else stop.
I don't know if this will be very efficient, it depends on your number of entries in the database, but technically it's not expensive to just check if the current date matches a date fetched from the database + 12 hrs, I cannot say more because you didn't give too much details about your data.
You'd probably be better off with a cronjob
http://en.wikipedia.org/wiki/Cron
Potentially, you could look into MySQL event scheduler. Although It might not fit your needs, hard to really tell on the details given
http://dev.mysql.com/doc/refman/5.1/en/events.html
something like
CREATE EVENT myTimedEvent ON SCHEDULE EVERY INTERVAL 5 minutes DO CALL updateRows();
updateRows checks your criteria (12hours ago), if it is, perform whatever action you want to do. This requires your MySQL to be # version 5.1+ however
You would probably best have a cron job which runs every minute and checks for any rows in the database > 12 hours old that have not yet been processed the script and process them. This wont give you EXACTLY a 12 hour difference, but would give you a difference within a minute of that.
You would probably also want to make sure that script would be able to run within a few seconds such that you don't have overlap of the script running twice at the same time.
This could be done using CronJobs. If you have root access to your Server or a server administration toolkit that offers cronjob managment you would do it on your server. otherwise use an online cronjob service (google for cronjob online).
the cronjob then triggers a php script on your server in your defined interval, like every minute or every 5 minutes.
this script then selects all rows from your mysql table which are older then 12 hours (WHERE timestamp <= NOW() - INTERVAL 12 hour ) and performs your desired actions on each result and delete the result from the table (or mark it done),
just make sure that the fetching and the actions itself are faster than your cronjob interval, otherwise you would have two or more scripts working on the same rows.
The easy way for me is to make the page head contains
<head>
<meta http-equiv="refresh" content="43200"> <!-- 43200 for (12 hours × 60 mintes × 60 seconds) -->
</head>
This method is very helpful to avoid server time out, which you can't avoid if you are using only PHP code
Important if you start the script using submit button, it's recommended to make the form action="other_page" not the same page, and sure you should save your input values as cookie using setcookie function and grab it as a variable in the action page using $cookieVar = $_COOKIE['the_cookie_name'] ;
You may need to increase or decrease the $cookieVar value and update the value again using setcookie every time your head code http-equiv do the refresh automatically after the specific seconds (43200 in our example) depends on what you want to do
Note that if you make your action start automatically without pressing submit button you can do the action in the same page.
I did that idea before, I had 180,000 images in one folder and the server didn't allow me to download it all because it was showing me only first 7998 images, so I create a script to zip each 1000 image in a file outside the images folder and to avoid time out, I made the code refresh each 60 second , finally I got only 180 zip file :)

When do I need to use cron?

Lets say there is a thread ( on forums ) which will be active for 3 days only. Now, after 3 days, I want this thread automatically closed.
Can I use reference to time when this thread is created in database, and than make if statement if current date + days is bigger than date created, I will print out "<h2>Thread Closed for posting</h2>"
And when I consider some other tasks, I suppose I can use reference to time and have certain event executed on this.
Am I right?
You can use a cron (ran every minute) to set a status field on the thread table to closed such as.
UPDATE threads
SET status='closed'
WHERE lastPost+INTERVAL 3 DAY<NOW()
Then in PHP something such as
if($thread['status'] == 'closed')
{
// Put your HTML here.
}
A 'cron' is a task that runs at a specific interval or time. This means that it should be used for tasks that must be done without user interaction. For example, a backup, automated emails or pulling data from a remote service.
What you want is better suited to a condition on checking whether a thread is closed. Rather than just having a flag, you also check the age. This means you can change your old-thread logic without needing to edit the database.
You could make a PHP script that gets executed by cron (read up on how to execute PHP in the command line) that SELECTs all the posts in a certain date and then sets them to closed. If you ran that, say, twice a day, then you could do a good job in getting all those posts closed.
Good reference on using cron to run PHP

Categories