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
Related
This is driving me crazy!!!
Here is the code that I am trying to put data for the last 31 days between 2 dates, NOW and 31 days later.
This code does not work:
$now = date("Y-m-d");
$datetime = new DateTime($now);
$datetime->modify('+31 days');
$NEW_30 = $datetime->format('Y-m-d');
$get_time1ax = "select * from support_tickets WHERE start_date >= '".$now."' AND end_date <= '".$NEW_30."' ORDER BY problem_title ASC";
But this code does work? Do not understand this.
$get_time1ax = "select * from support_tickets WHERE start_date >= '2020-03-19' AND end_date <= '2020-04-19' ORDER BY problem_title ASC";
Notice the actual date is in the field and not a variable. Weird.
Any help would be helpful.
I am trying to put data for the last 31 days between [...] now and 31 days later.
You can do date arithmetics directly in the database:
select *
from support_tickets
where start_date >= curent_date and end_date <= current_date + interval 31 day
Possibly, you mean 1 month when you say 31 days, so:
select *
from support_tickets
where start_date >= curent_date and end_date <= current_date + interval 1 month
try this:
$current_date = date("Y-m-d");
$current_date_plus_31 = date('Y-m-d',strtotime('+31 days',strtotime($current_date)));
this is how it works for me with today's date and in 31 days
I am working on a report module. I have worked on comparing this week and last weeks report for certain agents.
This weeks Report query is as follows
SELECT COUNT(created_at) AS cust_count_new, agency_id, created_at FROM customers WHERE
(customers.created_at >= DATE(NOW()) - INTERVAL 6 DAY AND customers.created_at
< DATE(NOW()) + INTERVAL 1 DAY
Last Week Report query is as follows
SELECT COUNT(created_at) AS cust_count_old, agency_id, created_at FROM customers WHERE
(customers.created_at >= DATE(NOW()) - INTERVAL 13 DAY AND customers.created_at
< DATE(NOW()) - INTERVAL 6 DAY
What I am doing is comparing this weeks and last weeks report. Now how can i change the query to this month and last months (30 days). I am little confused, so any help is appreciated.
You can use MONTH as unit in the INTERVAL. Also, you can use CURDATE() instead of DATE(NOW()), to get the current date.
This Month Report query will be:
SELECT COUNT(created_at) AS cust_count_new,
agency_id,
created_at
FROM customers
WHERE customers.created_at >= CURDATE() - INTERVAL 1 MONTH AND
customers.created_at < CURDATE() + INTERVAL 1 DAY
Last Month Report query is as follows
SELECT COUNT(created_at) AS cust_count_new,
agency_id,
created_at
FROM customers
WHERE customers.created_at >= CURDATE() - INTERVAL 2 MONTH AND
customers.created_at < (CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY
I am using below code for fetching next monday but i am unable to fetch next sunday on same week. like today is friday and if i search next week then i need to get next week monday to sunday.
SELECT now() + INTERVAL 7 - weekday(now()) DAY
This above give me next monday.
I need to add 7 more days and get sunday date in same format.
2018-07-30 14:45:43 Monday
2018-08-05 14:45:43 Sunday
The strtotime() function is great for vague relative queries like this:
$monday = date('Y-m-d', strtotime('next monday'));
$sunday = date('Y-m-d', strtotime('next monday + 6 days'));
Yields:
2018-07-30
2018-08-05
Add 6 days to get the sunday of the monday. Add 13 days to add the second sunday after the monday:
SELECT (now() + INTERVAL 7 - weekday(now()) DAY) + INTERVAL 6 DAY;
If I understand correctly, you have gotten "2018-07-30 14:45:43" and need to get Sunday. Sunday is six days after Monday, so just add 6 to 7 and get 13.
SELECT now() + INTERVAL 13 - weekday(now()) DAY
How about
select monday, monday + interval 6 day as sunday from
(select now() + INTERVAL 7 - weekday(now()) DAY as monday) s1
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 hope following query will give you the idea what I am looking for-
SELECT SUM(t1.hours) AS totalhours FROM
(
SELECT (time_to_sec(timediff(time_out, time_in)) / 3600) AS hours FROM bb_work_log
WHERE user_id = 6 AND (working_date BETWEEN '2014-04-01' AND '2014-04-31')
) AS t1
In my query, you can see the working_date which I given here manually. But, I would not like to do it manually. I would like to pick first day and last day of current month dynamically.
First day of Previous Month
select last_day(curdate() - interval 2 month) + interval 1 day
Last day of Previous Month
select last_day(curdate() - interval 1 month)
First day of Current Month
select last_day(curdate() - interval 1 month) + interval 1 day
Last day of Current Month
select last_day(curdate())
You can use LAST_DAY(NOW() - INTERVAL 1 MONTH) + INTERVAL 1 DAY,which will subtract one month from now and by by adding 1 day in LAST_DAY of previous month will give you the first day of current month
SELECT SUM(t1.hours) AS totalhours FROM
(
SELECT (time_to_sec(timediff(time_out, time_in)) / 3600) AS hours FROM bb_work_log
WHERE user_id = 6
AND (working_date BETWEEN LAST_DAY(NOW() - INTERVAL 1 MONTH)
AND LAST_DAY(NOW()))
) AS t1
LAST_DAY(NOW() - INTERVAL 1 MONTH) this will give you the last day of
previous month
First/Last day of Month Fiddle Demo
You can achieve it these ways ----
/* Current month*/
SELECT DATE_SUB(LAST_DAY(NOW()),INTERVAL DAY(LAST_DAY(NOW()))-1 DAY),CONCAT(LAST_DAY(NOW()),' 23:59:59');
SELECT LAST_DAY(CURDATE()) - INTERVAL DAY(LAST_DAY(CURDATE()))-1 DAY ,CONCAT(LAST_DAY(NOW()),' 23:59:59');
/* previous month*/
SELECT DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH,'%Y-%m-01 00:00:00'),DATE_FORMAT(LAST_DAY(CURDATE()-INTERVAL 1 MONTH),'%Y-%m-%d 23:59:59');
-- first day of previous month
set #start_date = date_format(NOW() - INTERVAL 1 MONTH, '%Y-%m-01');
-- last day of previous month
set #end_date = date_format(NOW() , '%Y-%m-01') - INTERVAL 1 day;
select #start_date ,#end_date ;
-- first day of current month
set #start_date = date_format(NOW(), '%Y-%m-01');
-- last dat of current month
set #end_date = date_format(NOW() + INTERVAL 1 MONTH, '%Y-%m-01') - INTERVAL 1 day;
select #start_date ,#end_date ;