Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We have a database table with created datetime and status flag. We would like to update the status flag to the next status if the created date time has elapsed by 30 minutes without any user intervention. How can we achieve this in php.
Create a php file that does this status change in the database and program to execute it every 30 minutes with cron (Linux) or Task Scheduler (Windows).
You have to think carefully about how you design and use your database. Sometimes things are made overly complicated when they don't need to. For instance, in this case you could use a 'datetime' in your table indicating the start time. Any PHP script can now check whether or not the start time started 30 minutes ago, only when this information is actually needed. No need for a flag, cron jobs, etc.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I will try to explain what I am trying to achieve. I am writing laravel json api with MySQL. Imagine user creates record inside my database. What I want to do is perform some kind of operation 24 hours after this record is created. It's something like cron job but what will be the the best solution? Should I run cron for example every 5 minutes and check if expired 24 hours for any of the records? Or maybe there is better solution?
You should use the Laravel queue system for this. When you insert a record in the database, you should create a new delayed job. Set the delay to 24 hours and Laravel will execute the job at that moment.
AfterInsertJob::dispatch()->delay(now()->addHours(24));
cron is fine, but (mysql) databases have events, which can be scheduled. In my opinion, this is really down to your personal taste and therefore more or less off-topic here. I mean an update after 24 hours on the entries is one SQL statement which should be rather fast, unless you have millions of entries ;)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I currently have a MySQL database that sends an auto email each time a new record is entered. I need to also send a reminder email to the email address in each row 5 days after it is inserted. What is the best way to do this? Can I use a MySQL trigger or a cron job? I will likely need step by step instructions, if it isn't through PHP. I've done quite a bit of searching but can't find exactly what I'm looking for.
A cron job is what you need. The php file doing the cron needs to query the table for rows where the entry date is equal to 5 days ago. The mysql query is:
SELECT `email` FROM `entries` WHERE date(`entrytimestamp`) = date_sub(curdate(), INVERVAL 5 DAY);
Hopefully this gets you started in the right direction. You'll need to make sure that the php file cannot be called outside of cron, that cron only happens once a day, or maybe keep a separate table logging the emails where you would add a "NOT IN email_log" condition to make sure that you don't double up on emails to someone.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to do on a script I`m working on that every month a part of the site resets.
Or a better example, Something like a Subscription, when you want to buy something and you need to renew it every month. How can I know its been a month?
In PHP you can't do regular cron jobs, and I will discourage you seriously from doing it with real cron jobs if you don't know what you're doing.
You can only register when you last executed that event and then check if it's been a month since then. This is a really simple sample cron:
<?php
$lastexecution = /*logic to know when you last executed.*/;
/* It's either a database or a file or something similar.
* I usually use a database table that contains the records when I
* last executed a cron
*/
if (time() > $lastexecution + (30 * 24 * 3600)) {
/*CRON LOGIC*/
}
You also should look into flock() or some similar locking mechanism to prevent the cron being triggered by two different users simultaneously.
Note: In your case, with a subscription, you could add a expires field to your database that would contain the date and time when the user's subscription needs to be renewed. If that date is in the past, you tell them
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a Twitter App and twitter makes it so any application won't run more than 15 times an hour.
I execute a python script when a user presses a button that gets the top 5 trends on twitter.
I am using PHP and MongoDB NoSQL to store my data.
When I searched for an answer I came across --> this but they are using a SQL DB.
My Question,
How can I tell the user has executed the script 15 times within an hour?
Each time the user runs the app, add their clicks to an object including its time.
When they run your script, pull these objects out, look through their dates, and delete everything with a timestamp more than an hour old and count the rest.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to run a php function on my server in a certain time without make the script sleep.
I need this in order to update my database in specific time.
For example a chat room that lasts for 5 minutes.
The chat room have a flag in the database for open status.
I want to change the flag in the database to close after 5 minutes.
public function callOnDelay($time, $data)
{
//SOME CODE EXECUTED AFTER SOME TIME IS OVER//
}
There are different ways of doing this depending on your OS.
On Linux you can look for Cron Jobs (http://www.thesitewizard.com/general/set-cron-job.shtml).
On Windows you can look for Task Scheduler (http://technet.microsoft.com/en-us/library/cc766428.aspx).
Also another, not so reliable, method of doing this is adding a conditional/if in your main script (index.php ?) or a "before function" in your controller (if you are using some kind of framework that supports it) and checking for the last status and doing something with it.