Hi i have this code that I would like to get the total of log that is closed.
(A) More than 2 days.
(B) Less than 2 days.
for($m=1; $m<=12; ++$m) {
$next_year = $cur_year + 1;
$monthName=date('M', mktime(0, 0, 0, $m, 1)).'';
$monthNumber = date('m', mktime(0, 0, 0, $m, 1));
$monthName = date('F', mktime(0, 0, 0, $m, 1));
$currentMonthText = date('Y-m-d', mktime(0, 0, 0, $m, 1));
$nextMonthText = date('Y-m-d', mktime(0, 0, 0, $m + 1, 1));
$result= $DB->query("SELECT * FROM ".$DB->prefix("calldesk_log")."
WHERE MONTH(date_open)='$monthNumber' AND date_open >= '$currentMonthText'
AND date_open < '$nextMonthText' AND status='Closed'") or die(mysql_error());
while($row = $DB->fetchArray($result))
{
$id_report=$row['id_report)'];
$date_open=$row['date_open'];
$date_close=$row['date_close'];
$diff = abs(strtotime($date_open) - strtotime($date_close));
$min = floor($diff / (60*60*24));
}
}
Any help will be appreciated.
If its only a matter of days differences, let MYSQL do the job using DATE_ADD() Function, you will thus avoid classical date mistakes which are obvious in your actual code (leap years, year change, etc...)
Here are some examples depending on what you want to do , which is unclear...
Logs that are between 2 days in the past and 2 days in the future (if that's possible but this what you do in your code)
SELECT *
FROM ".$DB->prefix("calldesk_log")."
WHERE date_open BETWEEN DATE_ADD(curdate(), INTERVAL -2 DAY)
AND DATE_ADD(curdate(), INTERVAL 2 DAY)
AND status='Closed'
Logs that are between current date and 2 days in the future
SELECT *
FROM ".$DB->prefix("calldesk_log")."
WHERE date_open BETWEEN curdate()
AND DATE_ADD(curdate(), INTERVAL 2 DAY)
AND status='Closed'
Logs that are between 2 days in the past and current date
SELECT *
FROM ".$DB->prefix("calldesk_log")."
WHERE date_open BETWEEN DATE_ADD(curdate(), INTERVAL -2 DAY)
AND curdate()
AND status='Closed'
If you want to count months instead of days, just change the INTERVAL:
DATE_ADD(curdate(), INTERVAL -2 MONTH)
If you want to take the time into account, use NOW() instead of curdate():
DATE_ADD(NOW(), INTERVAL -2 DAY)
That's it, you can do all this in pure SQL and let MySQL do the job for you.
Related
I am creating an algorithm that must return two intervals between two dates:
$interval_day (between 6am and 9pm)
$interval_night (between 9pm and 6am)
Currently, I managed to have the overall interval between these two dates for the day. However, I now need a daytime interval and a nighttime interval for the day.
Here is the code I have done to get the interval per day between two dates:
foreach($period as $p => $v){
$day = $v->format('Y-m-d');
$interval = 0;
foreach($rotations as $rotation){
// Get the day of each dates
$date_start = explode(" ", $rotation['date_start'])[0];
$date_end = explode(" ", $rotation['date_end'])[0];
if($date_start == $date_end){
// += interval between the two dates
$interval += strtotime($rotation['date_end']) - strtotime($rotation['date_start']);
}else if($date_start == $day){
// += interval between the start date and midnight
$interval += strtotime($day." 23:59:59") - strtotime($rotation['date_end']);
}else if($date_end == $day){
// += interval between midnight and the end date
$interval += strtotime($rotation['date_end']) - strtotime($day." 00:00:00");
}
}
}
I hope it's clear for you
What I want now is to get 2 intervals instead of one :
1 interval for dates between 6 am and 9 pm as $interval_day
1 interval for dates between 9 pm and 6 am as $interval_night
For exemple:
rotation['date_start'] = 27/07/2018 21:00:00
rotation['date_end'] = 28/07/2018 02:00:00
then
27/07/2018 :
$interval_day = 00:00:00 and $interval_night = 03:00:00
28/07/2018 :
$interval_day = 00:00:00 and $interval_night = 02:00:00
For each day you have to compute 3 overlappings:
overlapping between the interval [date_start time_start,
date_end time_end] for the given day and the interval [date_start 06:00:00, date_end 21:00:00]
overlapping between the interval [date_start time_start, date_end time_end] for the given day and the interval [date_start 00:00:00, date_end 06:00:00]
overlapping between the interval [date_start time_start, date_end time_end) for the given day and the interval [date_start 21:00:00, date_end 23:59:59]
To do that, use the following formula:
overlap = MAX(0,end_1 - start_1 - MAX(0,end_1 - end_2) - MAX(0,start_2 - start_1))
The formula needs integers - i.e. UNIX timestamps.
I am calculating weekly values comparing the last week and the same days in the year before. As we just have a leap year, my approach causes problems.
I am calculating the dates like this and perform a corresponding select:
$today_raw = date('Y-m-d');
$yearAgo = date('Y-m-d', strtotime('-1 year', strtotime($today_raw)));
$weekAgo = date('Y-m-d', strtotime('-6 day', strtotime($today_raw)));
$weekYearAgo = date('Y-m-d', strtotime('-1 year', strtotime($weekAgo)));
$stmt_currentWeek = $pdo->prepare("SELECT X FROM Y WHERE Z BETWEEN '$weekAgo' AND '$today'");
$stmt_weekLastYear = $pdo->prepare("SELECT X FROM Y WHERE Z BETWEEN '$weekYearAgo' AND '$yearAgo'");
It's obvious that the SELECT returns the wrong number of values as $weekYearAgo is simply wrong as it does not reflect the leap year differences.
What am I doing wrong?
Just use mysql DATE Functions:
$stmt_currentWeek = $pdo->prepare("SELECT X FROM Y WHERE Z >= DATE_SUB(NOW(), INTERVAL 1 WEEK)");
$stmt_weekLastYear = $pdo->prepare("SELECT X FROM Y WHERE Z BETWEEN DATE_SUB(DATE_SUB(NOW(), INTERVAL 1 YEAR), INTERVAL 1 WEEK) AND DATE_SUB(NOW(), INTERVAL 1 YEAR)");
1 Year And 1 Week AGO:
DATE_SUB(DATE_SUB(NOW(), INTERVAL 1 YEAR), INTERVAL 1 WEEK)
1 Year AGO:
DATE_SUB(NOW(), INTERVAL 1 YEAR)
1 Week AGO:
DATE_SUB(NOW(), INTERVAL 1 WEEK)
In the first SQL you dont need Between 1 WEEK AGO and NOW you can just rewrite it to >= 1 WEEK AGO
my graph is not reading between the correct dates that the data was inserted and instead its reading the amount of rows and displaying all the 29 days. Here is the code:
$graphTimeOne = time();
for($graphTimeOne = 29; $graphTimeOne >= 0; $graphTimeOne--){
$sel_timestamp = mktime(0, 0, 0, date("n"), date("j") - $graphTimeOne, date("Y"));
$graphResult = mysql_query("SELECT COUNT(id) FROM user WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 29 DAY))");
Basically its suppose to display how many are per day, but instead it displays the total on every day, the interval is 29 days. Any ideas how I can fix it?
Your SQL is incomplete - it is missing the day grouping:
SELECT COUNT(id)
FROM user
WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 29 DAY))
GROUP BY DAY(FROM_UNIXTIME(`timestamp`))
I want all data from last week.
I used
SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
but its not working when my week starts with "Monday".
What should I do?
SELECT id FROM tbl WHERE date >= CURDATE() - INTERVAL (WEEKDAY(CURDATE())+7) DAY AND date < CURDATE() - INTERVAL (WEEKDAY(CURDATE())) DAY
I try this and it work for me.
Try
SELECT id FROM tbl
WHERE YEARweek(date) = YEARweek(curdate())
Then change like this
$lastweek = unix_to_human(time("Y-m-d H:i:s") - (7 * 24 * 60 * 60), TRUE, 'us');
SELECT id FROM tbl
WHERE date >= curdate() - $lastweek
AND date < curdate() - $lastweek
I was just wondering how you can echo a range of dates in PHP
ideally i want the system to display this week wednesday to next week wednesday
for example
17/11/10 to 24/11/10
can anyone point me in the right direction?
$oBeginDate = new DateTime('last wednesday');
$oEndDate = new DateTime('next wednesday');
echo $oBeginDate->format('d/m/Y') . ' to ' . $oEndDate->format('d/m/Y');
i see no mention about mysql in your question, but if you're really talking about it:
select * from my_table where date_fld between '17/11/10' and '24/11/10'
create a date, and add days to it...
$m= date("m");
$de= date("d");
$y= date("Y");
for($i=0; $i<8; $i++){
echo date('d-m-y:D',mktime(0,0,0,$m,($de+$i),$y));
echo "<br>";
}
use strtotime
$now = '17/11/2010';
$next_week = date('d/m/Y', strtotime('+1 week', strtotime($now)));
Since MySQL is tagged...
SELECT now(),
case
when weekday(now()) = 3 then date_sub(now(), interval 1 day)
when weekday(now()) = 2 then curdate()
when weekday(now()) = 1 then date_add(now(), interval 1 day)
when weekday(now()) = 0 then date_add(now(), interval 2 day)
when weekday(now()) = 4 then date_sub(now(), interval 1 day)
when weekday(now()) = 5 then date_sub(now(), interval 2 day)
when weekday(now()) = 6 then date_sub(now(), interval 3 day)
end as current_wednesday,
case
when weekday(now()) = 3 then date_add(now(), interval 6 day)
when weekday(now()) = 2 then date_add(now(), interval 7 day)
when weekday(now()) = 1 then date_add(now(), interval 8 day)
when weekday(now()) = 0 then date_add(now(), interval 9 day)
when weekday(now()) = 4 then date_add(now(), interval 5 day)
when weekday(now()) = 5 then date_add(now(), interval 4 day)
when weekday(now()) = 6 then date_add(now(), interval 3 day)
end as next_wednesday