According to days change homepage or results - php

i have a two tables, days and posts.
Days table
id | day
----------
1 | Day1
2 | Day2
3 | Day3
4 | Day4
5 | Day5
6 | Day6
7 | Day7
8 | Day8
9 | Day9
10 | Day10
11 | Day11
12 | Day12
Posts table
id | day | posts
---------------------------
1 | 1 | Day1Text
2 | 1 | Day1Text
1 | 1 | Day1Text
2 | 2 | Day2Text
1 | 2 | Day2Text
2 | 3 | Day3Text
1 | 4 | Day4Text
2 | 5 | Day5Text
I have a 12 days and each days have a different posts, every day at 10.00 am this change, example Today(Day1) and only 1st day posts will appear. Tomorrow at 10.00 am 2nd day posts will appear. When the 12th day ends, this loop will return to the beginning and start again 1st day. How can i do this?

First off you want to know how to cycle over the posts as days go by. As input we need at least 12 different days, so we can use the current day of the month or year. If we take the day of the year:
Carbon::now()->dayOfYear //e.g. int(137)
We can now calculate its mod against the number of posts, which will always return a number between 0 and 11. If we add 1 we'll have a number between 1 - 12. E.g:
137 % 12 = 5 // 5 + 1 = 6
120 % 12 = 0 // 0 + 1 = 1
347 % 12 = 11 // 11 + 1 = 12
At this point, we know what post we need to display each day, but we're not controlling the hour it should start displaying.
To fix this, one way would be to consider the days change at the time you want the posts to change, aligning both makes things easier. As we don't want to offset the server timezone, we can just offset the date we pick by 10h meaning that a new day will begin once the current day gets to 10AM:
Carbon::now()->subHours(10)->dayOfYear
Now we can use the resulting mod value to build the necessary query to display the post.

From the looks of it you don't really need the if statement so for this you could either use the englishDayOfWeek getter or just use php's date format
i.e.
$results = Post::where('Day', now()->englishDayOfWeek)->get()
or
$results = Post::where('Day', now()->format('l'))->get()

Related

How to get data in past 30 days in MYSQL and Print Past 30 days inside a loop using a variable in php

I need to get past 30 days sales records from MYSQL database to display in a bar chart how many items sold each day. Also I need to display past 30 days in my chart x axis in my php document. I tried run this code inside a for loop. because I am running some other codes to display some data using this loop. Take a look at the following code and help me how to achieve this.
This is the table I try to get data from
Date format (YYYY-MM-DD)
Products Table
product_id | sold_by | qty | added_date | sold_date
-----------+---------+-----+-------------+-----------
3 | 12 | 7 | 2022-05-05 | 2022-07-28
3 | 12 | 7 | 2022-05-05 | 2022-07-29
3 | 12 | 7 | 2022-05-05 | 2022-07-30
3 | 12 | 1 | 2022-05-05 | 2022-07-30
3 | 12 | 2 | 2022-05-05 | 2022-07-30
6 | 22 | 4 | 2022-06-06 | 2022-07-31
8 | 11 | 6 | 2022-08-05 | 2022-07-31
Deleted Table
product_id | added_date | delete_date
-----------+-------------+-----------
3 | 2022-05-05 | 2022-07-28
3 | 2022-05-05 | 2022-07-29
3 | 2022-05-05 | 2022-07-30
3 | 2022-05-05 | 2022-07-30
3 | 2022-05-05 | 2022-07-30
6 | 2022-06-06 | 2022-07-31
8 | 2022-08-05 | 2022-07-31
As you can see for some days I have multiple records.
<?php
//for loop to run 30 times to get 30 days results
for ($i = 30; $i >= 0; $i--) {
$sold_products_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM products WHERE sold_date = CURRENT_DATE - $i"));
$deleted_count = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM deleted WHERE delete_date = CURRENT_DATE - $i"));
?>
{
<?php
$current_date = date("Y-m-d");
$new_date = date_create($current_date);
date_sub($new_date, date_interval_create_from_date_string("$i day"));
?>
x: '<?php echo date_format($new_date, "Y-m-d"); ?>',
a: <?php echo $deleted_count; ?>,
s: <?php echo $sold_products_count; ?>,
},
<?php
}
?>
x- X axis data in the chart
a - number of products deleted in specific date
s - number of products sold in specific date
With the above code I was able to print the x axis data. I am not sure how to get past 30 days data from the SQL database. I think the problem is in the $i part in the SQL query.
In your query, if you use INTERVAL, you can use some days for adding/reducing from the current or specific date.
For example in your case, if you always want to get the last 30 days data, you can use your WHERE like this:
sold_date >= CURDATE() - INTERVAL 30 DAY

need help in sql query with counting values within dates

the table:
tab_tasks
id|....|date_completed|completed
1 | | 2016-11-05 | Y
2 | | 2016-11-07 | N
3 | | 2016-11-09 | Y
4 | | 2016-12-11 | Y
5 | | 2017-01-15 | Y
6 | | 2017-01-30 | Y
and so on...
I need help in writing sql query by giving current date and get months with counted completed tasks back to max 12 months something like:
month | num_complited
november | 2
december | 1
january | 2
or if it would be easier the instead of letters of the alphabet, month can be numeric 11, 12, 01 in column month.
How can be this accomplished? Is there a need for any php code or can be this done with just sql query? Or should I add extra column to that table, like month?
you should put some date range otherwise for example 11-2016 has 6 count and 11-2017 has 5 so out put will show 11 count data mix-up
SELECT MONTHNAME(date_completed) as month ,
count(completed) as num_complited
from tab_tasks
where completed='Y' and
date_completed between '2016-11-05' and '2017-11-05'
group by MONTHNAME(date_completed)
Try this:
select MONTHNAME(date_completed) as MosName,count(1) as num_completed
from TableName
where completed = 'Y'
GROUP BY MONTHNAME(date_completed);

Getting and comparing averages from MySQL with PHP

I want to be able to contrast "last weeks" data against "this weeks" data and get the percent change. (i.e. +1.2% or -.5%)
Here's an example of the MySQL table:
Date | Happy | Sad | Angry | Fearful
2016-04-01 | 2 | 1 | 3 | 0
2016-04-02 | 3 | 1 | 3 | 1
2016-04-03 | 0 | 4 | 1 | 2
2016-04-04 | 1 | 3 | 2 | 1
So pretending that there are at least 14 rows here how would I go about getting the average of the first 7 days, the average of the previous 7 days, and then creating the comparison that shows the percentage difference?
I can get the most recent 7 days averages with the code below, but when I try and repeat it and change the offset it fails:
SELECT AVG(happy), AVG(sad), AVG(angry), AVG(fearful)
FROM table_name
LIMIT 0, 7
Use subqueries to calculate the averages for each week, using date ranges, and join them.
SELECT curWeek.happy AS curHappy, curWeek.sad AS curSad, curWeek.angry AS curAngry, curWeek.fearful AS curFearful,
prevWeek.happy AS prevHappy, prevWeek.sad AS prevSad, prevWeek.angry AS prevAngry, prevWeek.fearful AS prevFearful,
100*(curWeek.happy - prevWeek.happy)/prevWeek.happy AS happyChange,
100*(curWeek.sad - prevWeek.sad)/prevWeek.sad AS sadChange,
100*(curWeek.angry - prevWeek.angry)/prevWeek.angry AS angryChange,
100*(curWeek.fearful - prevWeek.fearful)/prevWeek.fearful AS fearfulChange
FROM (SELECT AVG(happy) AS happy, AVG(sad) AS sd, AVG(angry) AS angry, AVG(fearful) AS fearful
FROM tablename
WHERE date > NOW() - INTERVAL 1 WEEK) AS curWeek
JOIN (SELECT AVG(happy) AS happy, AVG(sad) AS sd, AVG(angry) AS angry, AVG(fearful) AS fearful
FROM tablename
WHERE date BETWEEN NOW() - INTERVAL 2 WEEK AND NOW() - INTERVAL 1 WEEK) AS prevWeek

How to find the duration of two time interval in php?

i have a Mysql table shift_def in the following format.
+-------------------------------------------------------------------------------+
| id | name | start_time | end_time | description | break |
|-------------------------------------------------------------------------------|
| 101 | Shift1 | 01:03:55 | 06:00:55 | Shift 1 | 1 |
|-------------------------------------------------------------------------------|
| 102 | Shift2 | 06:03:55 | 01:00:55 | Shift 2 | 3 |
+-------------------------------------------------------------------------------+
to find the total shift duration. i perform the following process in my php.
$shift_time = mysql_query("select start_time, end_time from rpt_shift_def where name ='Shif1'")or die(mysql_error());
while ($row = mysql_fetch_assoc($shift_time))
{
$Total_shift_time = strtotime($row['end_time']) - strtotime($row['start_time']);
$hours=floor($Total_shift_time/3600);
$Total_shift_time-=$hours*3600;
$minutes=floor($Total_shift_time/60);
$Total_shift_time-=$minutes*60;
$seconds=$Total_shift_time;
$Total_shift_time=$hours.":".$minutes.":".$seconds;*/
echo "$Total_shift_time";
echo "</br>";
}
the output is
4:57:0
when i try the same for shift 2 the expected output is
18:57:0
but the output is
-6:57:0
the negative value is because end_time of the shift 2 is less than start_time.
how to solve this problem.
can anyone help me out.
If end time is less than start time, add 24 hours (the reason being that the shift ended on the following day).
Since you have already calculated $Total_shift_time, you can just check if that is negative.
if( $Total_shift_time < 0 ) {
$Total_shift_time += 86400; // Number of seconds in a day
}
Err... Make sure you do this after you first calculate the shift time and before you start calculating hours, minutes and seconds...

sum hits for every week in mysql

I have a table with number of page views per day. Something like this:
+------+------------+------+----------+
| id | date | hits | mangaID |
+------+------------+------+----------+
| 4876 | 1331843400 | 132 | 13 |
+------+------------+------+----------+
| 4876 | 1331929800 | 24 | 236 |
+------+------------+------+----------+
| 7653 | 1331929800 | 324 | 13 |
+------+------------+------+----------+
I'm trying to get sum hits from last week with the below code:
SELECT sum(hits) as hits FROM om_manga_views WHERE DATE_SUB(CURDATE(),INTERVAL 1 week) <= date and mangaID = '13'
My problem is that I'm storing date as time using strtotime in date's field as int type.
So how can i get what i want!?
Try this:
select sum(hits) hitCount from t
where from_unixtime(date) >= current_date() - interval 1 week and mangaId = 11
Here is the fiddle to play with.
I slightly changed your data because the records you provided are older than 7 days, so the sum would return 0.

Categories