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;
Related
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
Now, I've a problem with the following query:
SELECT *
FROM table
WHERE date > CURDATE()
OR date = CURDATE()
AND time > CURTIME()
It's return rows with date > of today but I need also rows with date of today but with time > of the current time.
You need to put the related clauses inside parenthesis:
SELECT *
FROM table
WHERE date > CURDATE()
OR (
date = CURDATE()
AND time > CURTIME()
)
You should use the appropriate date/time functions instead of complicating yourself with complex WHERE clauses:
SELECT * FROM TABLE
WHERE ADDTIME(date, time) > NOW()
More information on the ADDTIME function in this link.
I wish to show upcoming birthday records on dashboard. I have MySQL table 'contacts' which
has field dob (DATE YYYY-MM-DD). I want to retrieve all the records with birthday is coming
in next 15 days. e.g is: birthday is 1986-03-24 it should be in result.
Take a look at MySQL DATE_ADD and INTERVAL.
By using both as below you can get record that are coming in next 15 days.
SELECT * FROM user WHERE dob BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 15 DAY)
FOR birthrate comparison.
MAKEDATE(YEAR(NOW() + INTERVAL 15 DAY), DAYOFYEAR(birthday)) BETWEEN CURDATE() AND CURDATE() + INTERVAL 15 DAY
SQLFiddle Demo
You have to use the date in mysql query as:
SELECT FROM table_name WHERE timestamp_row BETWEEN
'2011-01-01 00:00:00' AND '2011-01-02 23:59:59';
You are doing the B'Day dates of users and thus hope this would be do the trick of formatting and selection of date
select id from my_table
where
timestamp < date_format(date_add(CURRENT_TIMESTAMP(), interval 1 day),
'%Y%m%d000000' ) AND timestamp >= date_format(CURRENT_TIMESTAMP(),
'%Y%m%d000000')
Can do by this way also
SELECT * FROM person WHERE IF ( MONTH( NOW() ) < 12,
MONTH( birthdate ) = MONTH( NOW() ) + 1, MONTH( birthdate ) = 1)
You can use this query. Try Out
SELECT * FROM TableName
WHERE REPLACE(BirthDate, DATEPART(YEAR, BirthDate), DATEPART(YEAR, GETDATE())) BETWEEN GETDATE() AND DATEADD(DAY, 15, GETDATE())
I have this query that looks up results from a database for the last twenty minutes, now i know how to look up in hours, days, etc, but is it possible to look up only as far back as midnight of that day. so when ever the query is run and what ever time it only looks back as far as midnight?
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) and ip='$ip'
This is my code but is there away in which i can replace the interval for a specific time.
Any help would be appreciated.
Looking back to midnight of the current day is the same as looking at the current date with no time component. You can therefore use DATE() to truncate the datetime column date to only the date portion, and compare it to CURDATE().
SELECT * FROM ip_stats WHERE DATE(date) = CURDATE() and ip='$ip'
SELECT * FROM ip_stats WHERE date >= ( NOW() - INTERVAL 20 MINUTE ) AND date >= CURDATE() and ip='$ip'
Just make the column date be bigger than the curdate (which is the starting of this day).
SELECT * FROM ip_stats
WHERE date >= ( NOW() - INTERVAL 20 MINUTE )
and date > CURDATE()
and ip='$ip'
You can use CURDATE() to get rows since midnight of the current day:
SELECT * FROM ip_stats WHERE date >= CURDATE() and ip='$ip'
SELECT *
FROM ip_stats
WHERE date >= GREATEST( NOW() - INTERVAL 20 MINUTE, CURRENT_DATE )
AND ip = '$ip'
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.