I am trying to get a query to pull records from last week from thursday morning at 12:00AM to Wednesday night at 12:00pm.
This needs to always run and be current. I have another query for the current week from thursday to wednesday and it actually works. I just need another query that shows results after the week has passed from last week.
I believe this query is for last week but not starting on Thursday and ending on Wednesday.
SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo
WHERE thedate >= Curdate() - INTERVAL Dayofweek(Curdate())+6 day
AND thedate < Curdate() - INTERVAL Dayofweek(Curdate())-1 day
GROUP BY collector ORDER BY totalsum DESC
There might be a better way to do this using MySQL's own date functions, but I think I would probably just do it like this. I'm assuming that you meant Wednesday night at 11:59:59pm though, not noon:
$lastThursday = new DateTime("Thursday last week");
$wednesday = new DateTime("Wednesday");
$today = new DateTime("now");
$day = $today->format("l");
if ($day == "Sunday" || $day == "Monday" || $day == "Tuesday" || $day == "Wednesday") {
$lastThursday->modify("-1 week");
$wednesday->modify("-1 week");
}
$query = "SELECT collector, SUM('amountcollected') as 'totalsum' FROM commissioninfo WHERE thedate BETWEEN ".$lastThursday->format("Y-m-d")." AND ".$wednesday->format("Y-m-d ")." GROUP BY collector ORDER BY totalsum DESC";
Related
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
how would I go about echo'ing the first and last day of the current month? I assume I would use mktime() but I am slightly confused by it.
For example the current month is June so I am looking for a way to echo:
2016-06-01 00:00:00
And
2016-06-30 00:00:00
Thanks in advance.
You can use DateTime for this.
<?php
$start_date_month = new DateTime("first day of last month");
$end_date_month = new DateTime("last day of last month");
echo $start_date_month->format('Y-m-d');
echo $end_date_month->format('Y-m-d');
?>
Here is one method:
select date_sub(curdate(), interval 1 - day(curdate()) day) as month_start,
date_sub(date_add(curdate(), interval 1 month), interval - day(curdate()) day) as month_end
SELECT ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)),1) first,
LAST_DAY(CURDATE()) last
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
$qry = "SELECT * FROM school WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW()) ";
This query that i have currently in my code is getting me data from the current week starting from Monday. How can i get data from the table from last week or the week before. I have tried changing now to last week or currentweek.
any ideas is it possible to use weekofyear(()) and tweak it
You can do some date shift right in mysql, replace NOW() with
NOW() - INTERVAL 1 WEEK
there is also date_sub for subtracting
date_sub(NOW(),INTERVAL 1 WEEK)
Try this.
$qry = "SELECT * FROM school WHERE studId = $sessionId AND studentId = $student
AND date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY"
$start = strtotime('this week');
$results =$wpdb->get_results("SELECT count( doctor_name ) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date >='". $start."'");
this is my code to get last week leads count from table doctor name and where date with in this week (means today is thusday then start from previous week)
it not working??
and have do same for function like last month ??
in my db i use this leads_date field as timestamp
you can use the date_sub function from mysql
get all records from last week
SELECT count(doctor_name) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date between date_sub(now(),INTERVAL 1 WEEK) and now()
get all records from last month
SELECT count(doctor_name) AS totalleads FROM `wp-leads-count` WHERE doctor_name ='Sasanthi' and leads_date between date_sub(now(),INTERVAL 1 MONTH) and now()
try
$daynumber = date('N', date('d'));// getting today day number
$prevweek = $daynumber+7; // starting from prev week
echo $prevdate = strtotime('-'.$prevweek.' days'); // prev week date
echo strtotime("-1 month"); // last month
For more :-
Getting last month's date in php
day of the week to day number (Monday = 1, Tuesday = 2)