Sql Query to find data only from last hour - php

I Have An table As Follow
I need Data Only From Last Hour. Following query i tried using Reference.
SELECT * FROM `user_otp`
WHERE `date` = '$todate'
AND datetime > DATEADD(HOUR, -1, GETDATE())
I Dont Know This But I tried Using This Refferance
But It Not Work For Me .
So Any Help Would be useful.

DATEADD and GETDATE() exist in SQL Server.
In MySQL, your conditions should be:
WHERE `datetime` > DATE_ADD(NOW(), INTERVAL -1 HOUR)
While date = '$todate' condition is redundant and should be removed.
Here's a documentation in MySQL DateTime.

Use DATE_SUB and NOW() functions in query
SELECT count(*) as lasthour_count
FROM user_otp
WHERE datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR);

Hi you can use DATE_SUB function for fetch last one hour data. I edited your code below -
SELECT * FROM `user_otp`
WHERE `date` = '$todate'
AND datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR);

datetime>=DATE_ADD(NOW(), INTERVAL -1 HOUR);
datetime>= DATE_sub(NOW(), INTERVAL 1 HOUR);
These are the two options you can use

Related

i have my value as 2018-05-03 11:02:00 in mysql i want to pass this in my where condition using php

In my table 'time' data is present on 2018-05-03 11:02:00 this datetime. So I want to get the data by passing this datetime.
Here is my code:
$query1=$this->db->query("SELECT sum(count) As total, sum(status) As
is_completed FROM `tasks` WHERE `staff_id` = '$datas[$i]' AND `time` >
DATE_SUB(CURDATE(), INTERVAL 1 HOUR)");
This code showing no results but there is a data present on this datetime. how
can i pass this datetime as where condition
CURDATE() returns a date only. So when you do DATE_SUB(CURDATE(), INTERVAL 1 HOUR), you are returning the equivalent of 23:00:00 Yesterday
It seems for what you are trying to do, it would be better to use NOW().
You can try the following query:
SELECT sum(count) AS total, sum(status) AS is_completed
FROM `tasks` WHERE `staff_id` = '$datas[$i]'
AND `time` > DATE_SUB(NOW(), INTERVAL 1 HOUR)
Change CUR_DATE() to NOW()
$query1=$this->db->query("SELECT sum(count) As total, sum(status) As is_completed FROM `tasks` WHERE `staff_id` = '$datas[$i]' AND `time` > DATE_SUB(NOW(), INTERVAL 1 HOUR)");

PHP & SQL - compare unix timestamp with MySQL CURDATE()

i'm trying to do the following thing:
In my Database, I have rows with time in each one (using time() function).
For example: 1380300397.
Now, I'd like to build a query that gets the rows from the current day.
I've tried this query with no success:
SELECT `id`,DATE_FORMAT(`time`, '%Y-%m-%d') FROM `facts`
WHERE `app` = 1 AND DATE(`time`) = CURDATE()
What am I doing wrong?
Thanks!
AND DATE(FROM_UNIXTIME(time)) = CURDATE()
Well you can try use NOW() instead of CURDATE
In my opinion this is the best way to do it. I know it seems to be an overhead at the first glance, but this way you are not applying any function to your column, therefore your query can use index, if you have one on your time column.
SELECT
`id`, DATE_FORMAT(`time`, '%Y-%m-%d')
FROM
`facts`
WHERE
`app` = 1
AND `time` >= UNIX_TIMESTAMP(CONCAT(CURDATE(), ' 00:00:00'))
AND `time` < UNIX_TIMESTAMP(CONCAT(DATE_ADD(CURDATE(), INTERVAL 1 DAY),
' 00:00:00'))

How to compare two dates in MySQL query?

I want to compare two date in sql statement like following:
$before_5_days = date("d.m.Y", strtotime( '-5 days' ) );
$GLOBALS['db']->query("DELETE FROM matches_of_comments WHERE flag=1 AND match_date >'$before_5_days'");
the above query delete wrong fields.
the match_date column is in varchar and i can not change it , i want to compare it with $before_5_days in the same format . is there any function like "strtotime" that could be used in the query to change the type of the match_date and make the comparison???
DELETE FROM matches_of_comments WHERE flag=1 AND
DATE_FORMAT(match_date, '%Y-%m-%d') > DATE_SUB(CURDATE(), INTERVAL -5
DAY)
You could use date_format and str_to_date, for your needs i think this will work:
DELETE FROM matches_of_comments WHERE flag=1 AND date_format(STR_TO_DATE(match_date,'%d.%m.%Y'), '%d.%m.%Y') > '$before_5_days'
You can do this in MySQL; no need to do any PHP date math:
DELETE FROM matches_of_comments
WHERE flag = 1
AND STR_TO_DATE(match_date, '%d.%m.%Y') > CURRENT_TIMESTAMP - INTERVAL 5 DAY;
CURRENT_TIMESTAMP bases "5 days ago" on the date and time. To base it on date only, use CURDATE() instead of CURRENT_TIMESTAMP:
DELETE FROM matches_of_comments
WHERE flag = 1
AND STR_TO_DATE(match_date, '%d.%m.%Y') > CURDATE() - INTERVAL 5 DAY;

Get data from database inserted in latest 7 days

I am in problem in mysql database data retrieving. I have to get latest data inserted within a week or latest 7 days. I just know get data of specific date, but no within a span of days.
Please anyone help me. I am new in mysql.
You are looking for INTERVAL. For example, this will find all users whose created_time is in last 7 days and you have field created_time to tracked date of creation of record
SELECT * from users where created_time > (NOW()-INTERVAL 7 DAY)
You can do it using below query.
SELECT *
FROM `table`
WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND
CURDATE()
OR
SELECT * FROM `table` WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)
OR
SELECT * FROM table WHERE DATEDIFF(NOW(),dateField) < 7
we can use DATE_SUB Mysql default function
select * from yourtable where date_of_insertion >= DATE_SUB(NOW(),INTERVAL 7 DAY)
<?php
$date = date("your_date_format", strtotime(- 7 days));
$result = mysqli_query($link, "SELECT * FROM table WHERE `date` > '$date'");
?>
Simple as that.
try :
select * from tablename where add_time >= now() - interval 7 day

Get one day old record from database

How can i get one day old record from database from server . i have use datetime to insert record. below is how record is look like.
2013-01-15 23:44:02
i have use strtotime('-1 day') but it returns local system time.i want to get one day old record and do some stuff in condition..
Thanx in advance..
where date(date_column) = DATE_sub(NOW(), INTERVAL 1 DAY);
use DATE_ADD
SELECT *
FROM tablename
WHERE DATE(dateCol) = DATE_ADD(CURDATE(),INTERVAL -1 DAY)
DATE_ADD()
SELECT * from table WHERE datefield < DATE_SUB(NOW(), INTERVAL 1 DAY)
You will be archiving this with sql query.
SELECT * FROM tablename WHERE DATE(datefield)=DATE_ADD(CURDATE(),INTERVAL 1 DAY)
CURDATE() will return the current Mysql Server time.
You can use this function DATE_SUB(date,INTERVAL expr type)
http://www.w3schools.com/sql/func_date_sub.asp
Example provided in W3schools
Assume we have the following "Orders" table:
Columns: OrderId, ProductName, OrderDate
Values: 1, Jarlsberg Cheese, 2008-11-11 13:23:44.657
Now we want to subtract 5 days from the "OrderDate" date.
We use the following SELECT statement:
SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 5 DAY) AS SubtractDate
FROM Orders
$yesterday = strtotime('Y-m-d h:i:s','yesterday');
SELECT * FROM tablename WHERE datefield = '$yesterday'

Categories