I want a functionality that insert data every month...
I have some idea to implement this that is as follow....
When User Create 'INVOICE' at that time the 'INVOICE' automatically generated every next month once user create it.
Let have some code...
INSERT INTO INVOICE (id,user,date,bill_money) VALUES ('','XYZ','25/03/2015','100');
this one works very well now every next month same entry should be automatically inserted..
For that i have logic that when user log-in into portal at that time first i have retrieve user's last log-in and from that date to currently log-in date i try to insert those data which are 30 days old data...
I select next day(date) after user's last log-in date and then Check for every that up to current day(date).
To retrieve user last log-in date
echo $this->session->userdata('name')
To check interval lastlogindate() + INTERVAL 30 DAY == NOW()
If this becomes true then then insert data
But here problem is that i want to implement for month, quater, year
Is this flow is better or there can be another way to do this..??
I heard about cron job and MySQL Event which one is best in this two and how they are working which one is effective performance wise...
I want suggestion on this. Thank you....
If you want a recurring invoice. Just store the invoice once and schedule a cron job that will run daily at scheduled time. Cron job will run your php script to do whatever you want to do like : store invoice in db or email it to user. If you don't know about cron jobs, basics can be found in this answer : How to create cron job using PHP?
EDIT : you will have to schedule cron job using cli
Mysql events
I think this is better for me i haven't use Cron Job...
for Mysql event checkout syntax and have a fun...
Related
So here is what I am trying to accomplish:
User selects how often they want their post to be moved to the top of the page, whether it be every 30 minutes or every hour or every 2 hours, etc. They will be able to select how many times that it will do this. So if they select it to update every hour for 5 hours, it will then update the date/time in the database to the current time and then updates that time every hour for the next 5 hours and cancels it after the 5 hours are up.
I was thinking of running a PHP script like this to update the table since the ads are displayed by date DESC:
<?php
//cronjob.php
$id = $_SESSION['ad_id'];
$date = date('Y-m-d H:i:s');
$query = "UPDATE ads SET ad_date = :date WHERE ad_id = :id";
?>
Is there a best way to do that or will a CRON job that is selected by the user going to be too much load on the server to have users setting up CRON jobs continuously.
I saw this one approach and wonder if it is possible to set it up for this purpose. I am still getting familiar with CRON jobs.
How to start/stop a cronjob using PHP?
I will be setting this up on Godaddy's server:
Godaddy Cron jobs
Appreciate the help.
One method is to store the required updates in a new database table, containing fields such as the post id, how many times to update, interval, how many updates are left, the time at which the first update should take place ( or the time for next update ) .
Then, create a php script that fetches data from this table and take necessary action for each post. Check if it's time to update the post, if yes then update, else don't ( check time with accuracy upto minutes, don't check seconds. If you can, do this time checking with SQL and not in php to reduce the data being fetched ). It's better to perform all updates in a single query to maximise efficiency.
Then set up a cron job on the server that runs this php script every minute or whatever minimum time interval you need.
This way, you can get away with just one cron job😉
If you need even more efficiency, then at the end of this script, check if there are any more updates pending, if none then delete the cron job. Then, set up your code such that whenever a user creates a new update, check if the cron job exists, if it doesn't then create it ( this additional dynamic cron job is only for efficiency freaks. If there are frequent update requests, it might be better to avoid creating/deleting cron jobs )
I would like to store events' recurrences in a mySQL database (additionally, I'm working with Symfony 3 and Doctrine ORM).
For instance:
Every 3 days / Once a week / Bimonthly / Half-yearly / Once a year
What is the best way to store this kind of data in order to be able to easily perform queries on it?
Moreover, I want to create complex reminders based on these recurrences.
For instance:
From February to September: once a week and from October to January: bimonthly
How could I manage reminders' dates calculation? Should I store the start date and calculate the next dates each time, or should I store only the next date when a reminder is marked as completed?
(My use case: set reminders for watering plants).
Thank you a lot for sharing what do you think about!
Regards!
You can set events in MySQL itself or as pogeybait suggests, you can write a cron job and a command.
If you need the server to do something outside of the database, you likely need a cron job. If it's all contained within the database (such as just updating a "plants_need_watering = false" column to "plants_need_watering = true" without having to send an email or start some other program) then I'd say MySQL events are more suitable.
Here's a pretty good tutorial on events (although, I had to play around with the delimiters when I tried it): https://www.sitepoint.com/how-to-create-mysql-events/
Here's a simple event I wrote. You can see how it just updates a db column based on the date. I set the status of a my own "event" (not a MySQL event, but an entity from my application) to "Voting is closed." if the "voting_end" date is past today and this runs every 12 hours so I know I can't accidentally miss one. This is just entered once as regular SQL, you can practice and check results on a local dev machine by setting the schedule to every hour or so, check to see that it worked, then switch back to the actual desired timeframe to run the event: every week, every month, etc.
SET GLOBAL event_scheduler = ON;
CREATE EVENT switch_event_status
ON SCHEDULE EVERY 12 HOUR
DO
UPDATE event_status
INNER JOIN
event ON event_status.id = event.event_status_id
SET
event_status.value = \'Voting is closed.\'
where
event.voting_end <= cast(now() as date);
Also, here's the Symfony documentation on writing console commands. It's actually pretty easy. http://symfony.com/doc/current/console.html
And cron jobs to kick the console command off: https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
If you are on a Linux machine, you can use cron jobs to check however often as you'd like. Id create a console app to do this so the cron job is easier to call. It's easier than you think to create the console apps. For the database I'd store general info about each event (name, title, description) and an interval field which would say how often that even should trigger an notification. Initially store the current date and time when the event is added and then when the interval time passes trigger then notification and store the date and time of the notification. That's the simplest way I can think of. See more here http://symfony.com/doc/current/components/console.html for console commands.
I will take the timestamp enter by a user as $tmp1
and server timestamp as $tmp2
I want to run a mysql query when $tmp1-$tmp2==0
I can do this by user interaction whith server and check it every time any user login, whether $tmp1-$tmp2==0
but this will require redundant if statement every time .
can it be done using corn jobs or trigger I haven't used them before please help
if there are 100 users interacting with another 100 user this came as 100*100 checking for the time every minute you should check up when user login or you can do is crate a event table and hit that event on a particular time
if event_time< now() and event_executed=0 then hit
I'm trying to create a computer reservation system, where user chooses a computer and select the time how long he will be using this PC. In that time other persons can't reserve this pc, I need to find a solution, how to automaticaly delete all rows containing reserved pc's after their time expires. Thank you for the advice.
The common way to handle this is to store an expires_at timestamp on the reservation row. Then your query to find any "open" reservations would have WHERE 'expires_at' < NOW() or something similar.
This is an untested answer, that may only be a suggestion, but I just started looking at these, so am interested in feedback as well. i'm still working through possibilities and drawbacks, but it might well suit your need.
Take a look at MySQL Events, an article about it is here, and official syntax at Mysql Docs.
Per the article:
An event is similar to a trigger. However, rather than running in
response to a data change, events can be scheduled to run any number
of times during a specific period. In effect, it’s a database-only
cron job.
Pondering this, I'd envision a procedure that deleted anything >1hr (if that's the expiration). This procedure would be TRIGGERED on new inserts to get rid of anything expired at that moment, but also in an event to run every 15 minutes or so so that automatic deletes by the trigger aren't dependant on somebody else adding a reservation to trigger that procedure.
If your server is linux, you can use cron jobs to check once a day every reservation dates. If these dates have expired .. modified field reserves to be available.
Normally I would do it this way:
when storing a reservation, store date_from and date_to both of datatype DATETIME
when checking if there is a computer free check for all computers and filter with WHERE '{$my_date}' >= date_to AND '{$my_date}' <= date_from - by this You should be able to get all the PCs that are not reserved within a certain time...
To be complete in the solution, you need to run a CRON job which calls a query to remove all reservations that have a reservation_time + (15 * 60) < unix_timestamp().
I am assuming you have a time that the reservation was placed or started and are using UNIX/Epoch Timestamps.
Instead of doing a expires_now, if you know it will always be a fixed interval ie 15 minutes, you can do:
DELETE FROM reservations WHERE reservation_time + (15 * 60) < unix_timestamp()
Something you could look into is managing cron job's from PHP, http://www.highonphp.com/cron-job-manager.
The above script will, when a reservation is created, insert an entry into /etc/cron.d/ and you could configure it to run at the expected reservation endtime. Then inside the php file which would be executed, you could do:
DELETE FROM reservations WHERE id = :id
i am new in php.. i want to delete the user automatically if he does not attempt login in 30 days.
for example
if user login on "10-02-2012" ,
and if user doesnot login for next 30 days
then system should automatically delete his account.
if user again login on "15-02-2012" ,
then limit should be for next 30 days i-e "15-03-2012"
please help me i am very new in php
i have no idea how to store the date when user attempt to login.
You want to have a date field in the user table and a query that sets that date to CURDATE() whenever your login script runs. Something like:
UPDATE 'users' SET 'lastlogin' = CURDATE() WHERE 'userid' = '$userid';
Have a crontab that runs once a day (or however often you want) that queries all the fields that are 31 days old and deletes them:
DELETE FROM 'users' WHERE 'lastlogin' < CURDATE() - INTERVAL 31 DAY
Log the last login date in a Database.
Write a script which searches and deletes users where the last login was more then 30 days ago.
start the search and delete script with a cron job
Here's a tutorial on building a login system:
http://www.phpeasystep.com/phptu/6.html
Your solution would add to the tutorial by adding a DATETIME field named "last_login" to the members table. Whenever someone logs in, you update the last_login field with a database query like:
UPDATE TABLE members SET last_login = CURRENT_TIME WHERE id = xxx LIMIT 1
Then you can run another database query once a day to delete inactive accounts, customizing the deletion date as needed:
DELETE FROM TABLE members WHERE last_login < '2012-04-01 00:00:00'
it's very simple.
create a field in the DB table that stores the most recent login date.
write a script run every night at midnight that checks the login date against the current date.
the great thing about this is date objects allow you to easily compare dates easily.
here are some links that will help:
http://php.net/manual/en/function.date.php
http://www.w3schools.com/sql/sql_dates.asp
best of luck! it's pretty straight forward I have done it many times im sure you wont have much trouble.
In your data base, where user accounts are stored, you can store the last time they logged in using one of the built in MySQL date/time data types. In your PHP you can update this to the current time with another MySQL command. This page will get you started: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
As for deleting a user, you will want to set up a cron job/scheduled task, that will delete this user by checking the date and seeing if it is 30 days ago.
You can add two field for users table
created : date when user registered
login_date: date update on every login by user.
And you can use cron Job which run automatically (run a php file which path set in it) on selected time. you can run it every day on a fixed time. and if found both created and login_date same then delete that user from database. You can set cron job from your cpanel.
thanks
set up cron job for once a day and check who didn't logged in from last 30 days to current time / date you can use timestamp or date for last login