MYSQL get data within an certain time frame - php

hey everyone,
having problem getting this MYSQL query correct
SELECT *
FROM tbl_problem
WHERE user_id = 1
AND problem_solved != -1
AND problem_timestamp BETWEEN '20110212' AND DATE('20110212', INTERVAL 14 DAY)
ORDER BY problem_id
the problem is 20110212 is dynamically generated by php, which is why i use the DATE/INTERVAL COMBO.
what i am trying to do is select entries within a 2 week time frame given that you know the start date of that two week cycle.
thanks in advance

Why not just get PHP to reformat it and then query for
SELECT *
FROM tbl_problem
WHERE user_id = 1
AND problem_solved != -1
problem_timestamp > '2011-01-24' AND problem_timestamp < DATE_ADD('2011-02-12', INTERVAL 14 DAY)
ORDER BY problem_id
To convert the date in PHP from your current format to MYSQL's format, use
$date = date_create_from_format('Ymd', '20110212');
$mysqlDate = date_format($date, 'Y-m-d'); //2011-01-24

Use Datediff.
SELECT *
FROM tbl_problem
WHERE user_id = 1
AND problem_solved != -1
AND DATEDIFF(problem_timestamp,'20110112') <= 14
ORDER BY problem_id

Related

Calculate the difference b/w two months data

I have a mysql table orders in that I have a column order_date which is current time stamp(2016-08-17 00:00:00.000000). now I want to select or count the data's entered this month and the previous month, after this I can find the difference between these two months I am using this code and it is not working.
$sql="SELECT * FROM order WHERE order_date > DATE_SUB(NOW(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
this is not working an mysql error is produced.
Try
$sql="SELECT * FROM order WHERE DATE(order_date) LIKE DATE_SUB(CURDATE(), INTERVAL 1 MONTH)";
$result = $this->db->query($sql);
return $result;
i think this Link[http://sqlhints.com/2015/07/10/how-to-get-difference-between-two-dates-in-years-months-and-days-in-sql-server/] will help you
Use this. Hope it helps what you want. Thanks
$todayDate = date('Y-m-d');
$todayMonth = date("m", strtotime($todayDate ));
$previousMonth = $todayMonth - 1;
$sql = "SELECT * FROM order WHERE MONTH(order_date) BETWEEN '$todayMonth' AND '$previousMonth'";
First, the following is the correct logic to get all values from the current month and all of the previous month:
select *
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Then, use conditional aggregation for comparison. Here is an easy way:
select sum(month(order_date) = month(curdate())) as cur_month,
sum(month(order_date) <> month(curdate())) as prev_month,
(sum(month(order_date) = month(curdate())) -
sum(month(order_date) <> month(curdate()))
) as diff
from orders o
where order_date >= date_sub(date_sub(curdate(), interval day(curdate) - 1 day), interval 1 month);
Note: I don't fully see the utility of comparing a partial month (this month) to a full month (last month), but that is what you seem to be asking for. If you are asking for something different, then ask another question with sample data and desired results.

Get previous day date in MySQL format

I want to get count of previous day records from database.
I am using following method
$date = date('Y-m-d H:i:s', strtotime('-1 day'));
$users = 'SELECT Count(*) FROM users where date="'.$date.'"';
This is show count 0 as date format in database is (Y-m-d H:i:s).
Thanks.
Could just do
select count(*) from users where to_days(date) = (to_days(now()) - 1);
This is useful if your date column is a datetime - we're just converting to a day number and checking how many records have yesterdays day number.
Hope it will help you
SELECT COUNT(*) FROM users WHERE date = (CURDATE() - INTERVAL 1 DAY)
You might want to consider asking MYSQL itself about it, so that PHP doesn't have to compute it (and it is likely to be faster) :
SELECT Count(*) FROM users WHERE date = DATE_SUB(NOW(), INTERVAL 1 DAY)

Getting Newest Records from MySQL (7 days)

I am trying to grab the newest records from my table. I want all of the records that happened in the past 7 days. Here is what I have so far to start with.
$query = "SELECT * FROM mlg_logattempts AS a WHERE a.ts = (SELECT MAX(ts) FROM mlg_logattempts AS b WHERE b.ts > NOW() - INTERVAL 5 MINUTE AND b.name = a.name)";
I have used intervals in the past but an unsure how to make this work now. Can someone show me the proper way to request the past 7 days records? I do have a timestamp field.
UPDATE
Unfortunately I realized the command I shared with you. I do not have any of the above fields. The only date field I have is "date". no a or ts.
You could use mysql date_diff() for dates
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
but since you use timestamps, the interval is a good solution:
b.ts > unix_timestamp(CURDATE()-INTERVAL 7 DAY)
Supposing that the date of login attempt is b.ts and it's formatted like 2013-08-20 03:08:
$past7days = date("Y-m-d H:i:s",strtotime("-7day"));
$query = "SELECT * FROM mlg_logattempts AS a WHERE a.ts = (SELECT MAX(ts) FROM mlg_logattempts AS b WHERE date >= '$past7days' AND b.name = a.name)";

Show today orders from table

I do not understand how to get output from my sql table .
the table date is stored in int . for example date output from one row is like 1362157869 .
i want to show today orders in query :
php : $today = date("y-m-d", time());
query : SELECT * FROM test WHERE date = '$today'
but it didn't work . i also try this :
SELECT * FROM test WHERE date LIKE '$today'
Query without PHP var:
SELECT * FROM test WHERE date = DATE(NOW());
You are storing your date as a UNIXTIMESTAMP, so you have to convert it to DATETIME using FROM_UNIXTIME, and then you have to extract only the date part, ignoring the time, using the DATE() function:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE()
Please see fiddle here. You can then extract yesterday records with something like this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = CURDATE() - INTERVAL 1 DAY
Or a specific date with this:
SELECT * FROM test WHERE DATE(FROM_UNIXTIME(date)) = '2013-03-01'
To make use of an index
Assiming that your date column could contain not only the date part but also the time, you could also use this that will return all today records:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE())
and this for yesterday:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP(CURDATE() - INTERVAL 1 DAY)
AND date < UNIX_TIMESTAMP(CURDATE())
or this for a specific date:
SELECT * FROM test WHERE date >= UNIX_TIMESTAMP('2013-03-01')
AND date < UNIX_TIMESTAMP('2013-03-01' + INTERVAL 1 DAY)

Select all the records of the previous month in Zend Framework?

I have a small requirement in my project:
I want to fetch all the records of the previous month from the database.
The structure of the table is as follows:
id clientid task date
1 1 1 01.Feb.2011 12:13
2 1 1 05.Feb.2011 15:22
3 1 0 09.Feb.2011 14:17
4 2 1 11.Feb.2011 19:53
5 1 0 19.Feb.2011 14:27
6 2 1 23.Feb.2011 09:53
7 1 0 01.Mar.2011 14:17
8 2 1 01.Mar.2011 19:53
9 1 0 03.Mar.2011 14:67
10 2 1 03.Mar.2011 09:53
.....................
Here I want to fetch all the records of the previous month of a particular client in Zend Framework.
For Example : If I want client 1 records then It should show me records : 1,2,3 and 5.
Please Suggest some code, or link that helps me......
Thanks in advance
Assuming the date column is a DateTime column, I'd try with something like
$select->from('tablename')
->where('MONTH(date) = ?', date('n', strtotime('last month')))
->where('YEAR(date) = ?', date('Y'))
->where('clientid = ?', $clientId)
;
Note: untested and likely needs tweaking but it's the general direction
This would fetch all rows from tablename where the month is the last month and year is the current year and your clientId is the selected clientId. So the query should become something like
SELECT * from tablename
WHERE MONTH(date) = 2
AND YEAR(date) = 2011
AND clientid = 1;
You could also put the calculation for last month and current year directly into the query, e.g. using the appropriate MySql functions for this instead of calculating them with PHP. This might be more reliable.
You can get the first day of the current month using PHP:
$this_month = mktime(0, 0, 0, date("m"), 1, date("Y"));
$previous_month = mktime(0, 0, 0, date("m")-1, 1, date("Y"));
Then you simply pass this date as a parameter of your query:
SELECT * FROM mytable WHERE date >= ? AND date < ? and client_id = 1
where you replace the ? respectively by '$previous_month' and '$this_month'
If your date field is of the type Datetime you can use the date specific functions in MySQL to do this. Simply construct your statement with Zend_Db_Expr when using database functions.
My Zend_Db skill is a bit rusty, but I think the following does what you want:
$select->from('tablename')
->where(new Zend_Db_Expr('MONTH(`date`) = MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH))'))
->where(new Zend_Db_Expr('YEAR(`date`) = IF(MONTH(NOW()) = 1, YEAR(NOW()) - 1, YEAR(NOW()))'))
->where('clientid = ?', $clientId)
you can use SQL DATE_SUB and INTERVAL function like:
select * from table where `date` >= DATE_SUB(NOW(), INTERVAL 1 month)
In ZF1 you can write something like:
$select->from('tablename')
->where( 'date >= DATE_SUB(NOW(), INTERVAL 1 month)');

Categories