Regularly performed script [duplicate] - php

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.

Related

How to create a script that will run automatically every one hour and sends a curl request? [duplicate]

This question already has answers here:
How to set up a cron job to run an executable every hour?
(7 answers)
Closed 3 years ago.
I want to populate a hostoric database installed on a server. To do that I have to send multiple (curl)http requests on an endpoint changing the value of a certain field of a certain entity, also saved on a diffferent database. The problem is that I need to change the value of the field every hour for many days. I get it that people use cron jobs for this kind of task, but I need to know firstly in what language should I write the script preferrably(php, python, etc-with php being my personal preferred choice), or it does not simply matter and the important thing is to execute it via cron? Also what would the syntax for cron be in my scenario where I need to change the value every hour for say seven days?
Hi there are a lot of possibility to do cronjob, depends of your infrastructure. For example:
in a web-hosting with cpanel/plesk you can call a script (php for example)
you can also do cron by using user a starter, e.g. by creating a table on db with last execution on every user you can check the last execution and comparing it with actual time you can decide if start or not a scripts
you can create a crontab for your server in caso of managed server or vps (e.g. https://crontab-generator.org/)
you can use an online cron service. e.g. https://www.easycron.com/
and so on...

how to make a database timestamp relevant to user timezone [duplicate]

This question already has answers here:
Using time zones in a PHP web application
(2 answers)
Closed 7 years ago.
I have tried searching, but do not seem to be clearly getting an answer for my question, but it seems so simple it maybe my newbie skills are still too green to understand, so please go easy.
I have created a sports prediction comp on my website using a MySQL DB & PHP connection.
All works, but one issue I have is that my website has members from all over the world. How do I get the time of the game, and the cut off time for submitting predictions, to show in their own timezone? All matches in the DB are set to the local time for the match itself (which is GMT in this case, but isn't always the case).
Appreciate the assistance of you experts to help me improve my skills. Sorry if it's a common question, happy to be pointed towards a relevant link.
Thank you again.
RB
The most common method is to use a UTC timestamp, then when displaying the time, adjust it to the locale of the user. This requires that a local for each user has been provided. If it has not, note the UTC on the timestamp when shown to them.
The UTC method ensures that you can accurately determine the TRUE sequence of insert/update events, using the table data, vs having to examine txn logs.
You can let your users specify their location (time zone) within their preferences and your DB uses its own, then do the math

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

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.

How to update data in MySQL periodically? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is there an equivalent to cron in Windows?
How do I update my data in MySQL periodically?
Say I have table. One column is "Date". Another column is "Expiry_Status".
How do I update the table periodically(a few hours) such that, if the date in the "Date" column is after the current date, the "Expiry_Status" will be updated to "Yes" value.
I am using WAMP as the framework.
Edit: Thanks for the answers but a lot of you guys gave answers about changing the database architecture. The case I gave above is just a simple example. I am more interested on how databases are updated regularly. ie: the program used, the syntax, the paradigms....etc etc.
Thanks again.
You write a program to perform the update, and then run it using a schedule system.
On Windows (since you tagged the question WAMP), this is usually done with Task Scheduler.
Other operating systems usually use cron.
Changing a status column is redundant though. You can search for data where the expiry time is greater than NOW() instead of where the status is Yes. This sort of scheduled script is normally used when the old data is being deleted.
write a php script which checks times and updates appropriately then use windows task scheduler to run them at certain interval. (assuming windows os...)
What value does having this expiry_status even serve? If you have an index in that date/datetime field (which you would need to effectively update the expiry_status field anyway), you can just as easily query on...
WHERE `date` > NOW()
...in order to determine active records. That is in essence all you are going to be doing with this proposed update query anyway. Why maintain an extra field if you don't need to?

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