This question already has an answer here:
From the timestamp in SQL, selecting records from today, yesterday, this week, this month and between two dates php mysql
(1 answer)
Closed 8 years ago.
I want to show records of today (from yesterday 12 AM to today 11:59 PM), Yesterday, and records of this week,
I have this query for todays records
SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= NOW() AND `TimeStamp` >= ?????
I have a field in my table named TimeStamp format is 2014-09-20 12:11:20
Make your calculations based on CURDATE if you are selecting date only.
Hope the below Examples would help you
Today: WHERE timestamp >= CURDATE()
Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'
Please see this one too..
Use strtotime() and date() function for getting yesterday. Example....
$now = date('Y-m-d h:i:s', time());
$yesterday = date('Y-m-d h:i:s', strtotime('yesterday'));
$sql = "SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= '$now' AND `TimeStamp` >= '$yesterday'";
Also can use BETWEEN in query String. ie,.
$sql = "SELECT * FROM tblpatients WHERE Is_Deleted = '0' AND TimeStamp BETWEEN '$now' AND '$yesterday'";
Related
I am using this code:
WHERE date > DATE_SUB(CURDATE(), INTERVAL 3 DAY)
And result will be like this:
2022-05-12
2022-05-11
2022-05-10
But I want this:
2022-05-11
2022-05-10
2022-05-09
Use a range here:
WHERE date < CURDATE() AND date >= DATE_SUB(CURDATE(), INTERVAL 3 DAY)
Assuming today's date be 2022-05-12, the above logic would exclude this date but include the three previous days, from 11th May to 9th May.
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
$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"
This question already has answers here:
MySQL SELECT last few days?
(5 answers)
Mysql: results from last seven days
(2 answers)
Closed 8 years ago.
$today = date('Y-m-d');
$countreview= mysql_query("SELECT count(review),
time
FROM review
WHERE time BETWEEN ($today - INTERVAL 7 DAY) AND $today
GROUP BY time");
try this :
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)
try
time BETWEEN DATE_SUB(DATE( '$today'),INTERVAL 7 DAY) AND '$today'
if you want compare with current time use now()
time BETWEEN DATE_SUB(DATE( NOW()),INTERVAL 7 DAY) AND NOW()
Try this
$sql="SELECT * FROM review WHERE DATE(time) = DATE_SUB( CURDATE( ) , INTERVAL 7)";
I hope this is what you are searching for
Happy Coding!!
You can use this in your MySQL WHERE clause to return records that were created within the last 7 days/week:
created >= DATE_SUB(CURDATE(),INTERVAL 7 day)
Also use NOW() in the subtraction to give hh:mm:ss resolution. So to return records created exactly (to the second) within the last 24hrs, you could do:
created >= DATE_SUB(NOW(),INTERVAL 1 day)
I think you will find it simplest :
$sevenDayOld = date('Y-m-d', strtotime('-7 days 00:00:00'));
$SQL = "SELECT count(review),time, FROM review
WHERE time > '" . $sevenDayOld ."'
GROUP BY time";
Here is a query to get all rows in the past month.
$time = time() - 9676800;
$q = $this->db->query("
select id
from ipAddress
where date > {$time}
");
But how can i adjust this query to get all rows BESIDES the past month? Basically I want to end up deleting all rows over 1 month old
So long as your methodology for calculating "last/past month" satisfies you, then it's simple:
where date <= {$time}
You can use date_format, date_sub functions to get last month's dates.
Find answers here:
mysql last month date statement
and here
MySQL Query to calculate the Previous Month
You can do it like so
$time = strtotime('-1 Month');
$q = $this->db->query("
select id
from ipAddress
where `date` <= {$time}
");
but if date is a TIMESTAMP, DATE, or DATETIME string like 2013-02-27 22:16:38 or 2013-02-27 then you need something like
$time = date('Y-m-d H:i:s', strtotime('-1 Month'));
$q = $this->db->query("
select id
from ipAddress
where `date` <= '{$time}'
");
Or purely in SQL
select id
from ipAddress
where `date` <= DATE_SUB(NOW(), INTERVAL 1 MONTH)
And if I remember correctly, date is a reserved mysql word so use backticks in your sql.