I am trying to count how many days a user has clocked in within a certain date period, as the user can be clocked in for several days at a time i need to find the difference between the two dates and then count the total days for each column within the date period and only where the accountid matches.
Does anyone have any suggestions on where i am going wrong.
Database Table
id, datein, dateout, accountid
1, 2022-08-01, 2022-08-03, 1
2, 2022-08-04, 2022-08-05, 2
3, 2022-08-05, 2022-08-05, 1
4, 2022-08-05, 2022-08-08, 1
5, 2022-08-06, 2022-08-07, 2
6, 2022-08-06, 2022-08-08, 3
7, 2022-08-07, 2022-08-10, 1
8, 2022-08-08, 2022-08-08, 2
Code
<?php
$accountid =$user->data()->id;
$monthdate=date("Y-m-d", strtotime("-1 month"));
$crrntdte=date("Y-m-d");
$query3=mysqli_query($conn,"SELECT DATEDIFF('datein', 'dateout') as monthlysessions from addsession where ((datein) between '$monthdate' and '$crrntdte') && (accountid='$accountid');");
$result3=mysqli_fetch_array($query3);
$sum_monthly_sessions=$result3['monthlysessions'];
?>
<?php echo $sum_monthly_sessions;?>
if you want to get sum of previous 30 days you can just add SUM
before DATEDIFF
and if you want to make it consecutive
SUM(IF(DATEDIFF(dateout, datein) = 0, DATEDIFF(dateout, datein) +1, DATEDIFF(dateout, datein)))
you can use this, this kind of selection can be made better, but it's simplest. But if you want to make it like past month eg it's August, so from 2022-08-01 to 2022-08-31, use something like this:
SELECT
SUM(IF(DATEDIFF(dateout, datein) = 0, DATEDIFF(dateout, datein) + 1, DATEDIFF(dateout, datein)))
FROM
addsession
WHERE
accountid = 1
AND MONTH(datein) = MONTH(CURRENT_DATE());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.