Laravel4 Date based actions - php

I have a Mysql Table with columns below
ID Amount Charge_Date
1 200 12-Apr-2015
2 300 10-May-2015
I am trying to write code so that when date reaches today, the amount is debited in a new table. eg lets say today is 12 Apr then Rs 200 is debited to account which is a new table.
Please help me with the idea. Should I set a Cron Job which will run everyday and check if date is today. Or is there a better way of calculating such things.
Also need help with setting recurring events like everyday I need to calculate outstanding from a table and send email to customers.

Cron seems to be the only way out. Just need to set cron from cpanel and it will run for any desired controller

Related

How to calculate total online time using laravel?

I want to calculate particular user's total online time for selected date. Below is MySQL table -
User Active Status Table
In status column I store values as 0 (Offline) and 1 (Online).
As you see user_id 33 is online at -
10:05:25
14:00:00
and offline at -
11:45:58
19:00:04
So, How to calculate total online time using Laravel?
Thanks in advance!
You can try to refactor a bit your database..
Add 3 columns date, logged_in_time, logged_out_time and you can make an event listener each time user logs in our out. Any time they will log in our out it will be stored in your table and you can filter easily by dates in your query.
If you want to calculate the amount of time they were logged in for a specific date you will just do 'logged_out_time - logged_in_time'

How to automatically update an entry daily in phpMyAdmin?

I have a database called ARTICLES. Inside Articles, I have a table called Users. Inside Users, there is an entry called Renewal, which is an Integer.
Once Renewal is set, it should decrease by 1 every 24 hours. For example, let's say Renewal is 30. In 24 hours, Renewal should be 29. I do not wish to go inside the entry, and update this field every 24 hours. There will be hundreds of rows with different values for Renewal.
Is there a way to automatically update this entry every 24 hours, so that the entry decreases by 1?
You're taking the wrong approach. Instead of counting down in a complicated manner you can simply set an 'expire' date. 30 days from now? Then expire at 19 juli 2016 at 12:00. No need for automatic updates.
You can create scheduled events in MySQL - CREATE EVENT
I would check renewal values programatically in you PHP script based on your record timestamp instead of you approach.

Auto Insert In Database Every Month

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...

How to auto update account balance in database every 30 days

I have a customer table in my database. This table contains customer information including his/her balance.
Now I want to add his/her balance after every 30 days depends on what promo or plan he/she applied
example: he applied for a plan 1599 so after every 30 days his balance must add the price of the plan he applied.
current balance = 0
after 30 days balance = 1599
How will i do this?
You can create a cron job for the same. Check the date difference for every user and if its greater than 30 days add the balance to the respective user's account.
You can do this like
Store the Date when user choose plan by using date('Y-m-d') in a variable
Add 30 Days in it by using date('Y-m-d',strtotime("+7 day", $date)) and save this in database
Write a query to check that date of today is equal to that stored date or not if so then add points to that account.
For point 3 you may also need cron job depends on your requirement.
If still need help feel free to comment

Programming logic - update the database only one time after specific time interval

I am developing ecommerce store in php and I have some problem in creating a logic. The problem is I have a store page where I am showing some products. all the products have some time interval,after interval passes the products will no longer be display there.
For example
Product: jeans
time left: 10 days.
after 10 days jeans product will no longer be there. in database I have a set a field with the name active_status which accepts Y or N..
I know that I can simply run the update query and set the status to "N" after time passes. here in this example after 10 days
BUT the question is WHEN DO I RUN THIS UPDATE QUERY ?
should I always check time and run again and again update query and set STATUS TO 'N'??? IS that is the only solution ?
I mean usually we do like for example if customer logins we set some status or any other event but here we are setting the status against checking the time. Hopefully you have understand my question
In the db I am saving the start time and number of days which user puts through the admin panel
My first shot would be cron, php script and properties table (if needed, because for simple uses you could just store expiration date inside business entity).
Cron runs php script periodically (e.g. once a day),
scripts checks if there is anything to delete, based on properties table, or entity properties.
If there is anything to delete, script performs deletion of selected content.
That's all and it is in fact very popular scenario.
More on cron: http://www.pantz.org/software/cron/croninfo.html
Here is my logic i hope it will helps i think
While Publishing the product we have to maintain the time interval of that product for example if you want to display the product for 10 days give 10 days and date of publish product.
By comparing with that date and number of days given for time interval with the present date i.e today's date
Can you check with this

Categories