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'
I want to create a website with an scheduling calendar.
My first idea is to use some free calendar template or download some free scheduling calendar. Then in my scheduling form, when someone request for an schedule, I will get the date he/she input and save it into the database then show it to the scheduling calendar.
But someone told me that, in my database, I should create a calendar table.
Which is the best way around?
The first one with only one table for schedule on my database or the second one with two tables for schedule and calendar?
I hope you get my idea.
It could be first one. One of option is to keep data by day of year.
you can draw your own calendar by counting day of year
actual day of yaer - date('z') + 1; //+ 1 because it is an array it starts from 0
then you can get number of days in each month
cal_days_in_month
and loop it x 12 with
here will be day of month with css style so it looks like calendar field
your i++ will bee number of days in month of course.
Keep records in database by year and day of year. you can do so much things this way
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
I have a online course having 50 lessons.
Now what i want to achieve is that each lesson is shown 1 day at a time or every 24 hours depending on the date the members are subscribed?
Note:- members Can subscribed at different dates and times.
Any idea how can i achive that?
I am not sure this is a scheduled task, but more of a bit of logic in your php code that doesn't show the link for courses that are not accessible yet.
This should be fairly simple date processing based on a users registration date.
Here is a post on stack overflow for calculating date differences.
PHP date calculation
scenario:
there are 4 items:
30day pass
60day pass
180day pass
365day pass
there is a monthly (30 day) credit cap. so if you use the credit up before the end of the month you need to purchase another 30day pass, or wait till the next renewal period.
A person purchases a 30day pass today, purchase date is recorded to DB. Expiry date is also recorded.
if ($todaysdate >= $expirydate) //DONE.
but whAt about for 60day and 180day passes ?
we have purchasedate & expiry date. every 30days, the credit needs to be reset for the next month.
I am really lost how to best approach this problem.
one way to do that is with cron and mysql
when you run this cron every day
define a variable
define (DAY_AMOUNT ,30);
check the day pass by query
SELECT mem_id DATEDIFF(CURDATE(),mem_date) AS daypass FROM table
WHERE ((DATEDIFF(CURDATE(),mem_date))=".DAY_AMOUNT."
the results that return is all the members that pass 30 days from the date.
You could run a task (or cron job) daily to check and see if the current date is later than the recorded expiration date, and if it is reset the users credit to 0, and send them an email letting them know their credit has expired.
As the users buys credit, you merely add 30, 60 or 90 days to their expiration date.
For more information, see php + cronjob tags.
in your situation, you could have a table like this:
pass: (id, user_id, start_time, end_time, credit)
every day you can flush the unused credit on any pass that has expired:
update user join
(select user_id, sum(credit) as credit from pass
group by user_id where end_time <= now() and not flushed) b
set user.credit = max(user.credit - b.credit, 0)
where user.user_id = b.user_id
update pass set flushed = 1 where end_time <= now() and not flushed
by using end_time <= now() you ensure the credit is only reset once the expiration date has passed. since you remember which passes were flushed, you can run this as often as you like and missing a day is not an issue (it will be detected on the next run).
this is effectively how a prepaid calling card would work - you have a certain number of minutes that are valid for X (30/60/90 whatever) days and the minutes are reset at the end.
btw, you can simplify all that stuff by using an easy to use subscription management system like recurly or chargify.
Or, in the database, you can save each person has their own expiry date.
To make an expiry date, you should use a unix timestamp. 30 days is 2592000 so to find the expiry date, you could use something like the following if you want to calculate the expiry date (Changing months with how many months).
$months = 2;
$expiry_date = time() + 2529000*$months;
Then, put those values into a database.
Then, you can do something that does an automated task (for example, cron).
When you run that automated task, you check if the current timestamp (another word for time) is bigger then the expiry date. If so, then change the value in the database in the field for if they have the right credits.