get data from last week - php

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

Related

How to get records only for this week with timestamp in SQL?

I have a Table in Database which has records of Logins.
Table name: user_logins
ID | timestamp
1 2019.01.03 (Year, Month, Day)
2 2019.01.04
3 2019.01.05
4 2019.01.05
5 2019.01.07
6 2019.01.07
7 2019.01.09
I want to Show only Count of Records by this Week.
From Monday to Sunday (04-02-2019 ... 10-02-2019)
My PHP and SQL Code is:
$mo = mysql_num_rows(mysql_query('SELECT * FROM user_logins WHERE DAYNAME(DATE(timestamp)) = "monday" and timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE())-0 DAY)'));
this should show the records of 04-02-2019
Here is my SQL Fiddle link:
SQL Fiddle
This:
DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
gives this week's Monday.
So:
SELECT * FROM user_logins
WHERE
timestamp
BETWEEN DATE_ADD(CURDATE(), INTERVAL - WEEKDAY(CURDATE()) DAY)
and
NOW()
Try following query:
SELECT id FROM `user_logins`
WHERE timestamp >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND timestamp < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
Demo

PHP/MYSQL Compare Date this month and last Month

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

Puling out data from the table from last week

$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"

How to get all the records whose updated date is less than 30 days in php

I want to get all the records from mysql database who have updated their records within 30 days from the current date for that i have used the below query but it is not working properly. $tda is the current date and $prevmonth is the date of exactly
30 days back from the current date. Please help. Thanks.
$da=date('d');
$tda=date('d-m-Y');
$prevmonth = date(''.$da.'-m-Y', strtotime('-1 months'));
$sql_q=executeQuery("select * from ".reg." where 'uid' !=".$_SESSION['uid']." AND Updatedate >= '$prevmonth' AND Updatedate <='$tda '");
You can do it in mysql as
`Updatedate` < DATE(NOW() - INTERVAL 30 DAY)
OR
`Updatedate` < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
For Varchar
STR_TO_DATE(Updatedate, '%Y-%m-%d') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)
UPDATE :
The query posted in the comment is wrong and should be
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') < DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
If you are looking for data within last 30 days then
$sql_q=executeQuery("select * from registration
where
`uid` != ".$_SESSION['uid']."
AND STR_TO_DATE(Update_date, '%d-%m-%Y') >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)") ;
I like doing something like this: AND UpdateDate > NOW() - INTERVAL 30 DAY AND UpdateDate < NOW().
If your Updatedate column is a DATETIME column then you can do the following:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND Updatedate <= NOW();
Or:
SELECT *
FROM table
WHERE uid <> ?
AND Updatedate BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND NOW();
Or if it's a timestamp then this:
SELECT *
FROM table
WHERE uid <> ?
AND FROM_UNIXTIME(Updatedate) >= DATE_SUB(NOW(), INTERVAL 1 MONTH)
AND FROM_UNIXTIME(Updatedate) <= NOW();

HOw to SELECT * (all) which date start from the last day of last month to the first day of next month

use PHP and MySQL and want to use SELECT statement which date_post(datetime variable) start at the last date of last month and to date the first day of next month, help me please.
Thank you in advance.
my database: 'id', 'content', 'image', 'date_post',
etc. and I try to use
$today = getdate();
$thisyear=$today['year'];
$thismon=$today['mon'];
$date_start=$thisyear.'-'.$thismon.'-01';
$date_end=$thisyear.'-'.($thismon+1).'-01';
$sql="SELECT *, DATE_FORMAT(date_post, '%d-%m-%Y') AS datepost
FROM my_table
WHERE date_post BETWEEN date('$date_start')
AND date('$date_end')
ORDER BY date_post DESC";
It makes with one query in MySQL, without any PHP:
SELECT * FROM `table_name`
WHERE DATE(`date_post`) >= DATE_FORMAT(CURDATE() - INTERVAL 1 MONTH, CONCAT('%Y-%m-', DAY(LAST_DAY(CURDATE() - INTERVAL 1 MONTH))))
AND DATE(`date_post`) <= DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-01');
Ensuring that the query will not scan the full table but will use the index of date_post (if there is one!):
SELECT *
FROM myTable
WHERE date_post < LAST_DAY(CURDATE())
+ INTERVAL 2 DAY
AND date_Post >= LAST_DAY( LAST_DAY( CURDATE()) - INTERVAL 1 MONTH )
If it is run today ( 2011-07-01 ), it will give all datetimes between 2011-06-31 00:00:00 and 2011-08-01 23:59:59.

Categories