I'm setting up basic cookie tracking and validating that by making sure the cookie is on their computer AND their IP matches the record I stored AND the record was stored within the past hour. I'm getting hung up on selecting the mySQL data from within the past hour.
As it stands, the column in my table is called 'timestamp', and it just contains the full timestamp inserted with NOW(). I checked around and thought I found the right call, but this didn't work:
$q = "SELECT * FROM conversion_data WHERE ip='$ip' AND timestamp < DATEADD(HOUR, -1, CURRENT_TIMESTAMP) ";
Taking out the timestamp call, it all works fine, so it's just that one part.
Thanks!
Try:
$q = "SELECT * FROM conversion_data WHERE ip='$ip' AND timestamp > DATE_SUB(CURDATE(), INTERVAL 1 HOUR)";
Your current query will select rows that are older than one hour. Changing the timestamp predicate so that it will fetch rows that have a time that is newer than or equal to, i.e. greater than or equal to, should work.
$q = "SELECT * FROM conversion_data WHERE ip='$ip' AND timestamp >= DATEADD(HOUR, -1, CURRENT_TIMESTAMP) ";
Please note that I'm not sure if dateadd works like this for mysql, apply a relevant function in your case.
Related
Tonight I'm going to start working on a php script to set as a cron. The script is going to delete all the values of a particular column that are older than 20min. It's going to do this by checking the timestamp of when the value that will be deleted was entered. The timestamp will be stored as another column value on the table.
It seems like a pretty simple script to write, but my knowledge of SQL is lacking. I know I can compare the times in PHP, but I was just reading that it's possible to do it all in a SQL statement as well. If someone can please correct my delete statement it would be appreciated.
$query ="DELETE `key` FROM $table WHERE `time`<DATE_SUB(NOW(), INTERVAL 20 MINUTE);"
I need this statement to check the key value for the whole table, and I'm not exactly sure which PHP time function corresponds with the SQL comparison. Would I just use DateTime(); to set the timestamp?
$query = "DELETE `key` FROM $table WHERE `time` < ADDDATE(NOW(), INTERVAL -20 MINUTE)";
You can use the following query :
$query = "DELETE `key` FROM $table WHERE `time` < CURRENT YEAR TO MINUTE -20";
I have events in my MySQL database wich all have a date. When I perform a SQL query to get all the events in the future, I get an error... Although, the date of the events are in the future. When I change my SQL request to select dates in the past, I get those from the future...
The SQL statement below has worked before, but for some reason it stopped working...
I use this SQL request:
$sql = "SELECT * FROM calendar WHERE date >= CURDATE() order by `date`";
I get an empty array as result...
However if I change the query to this, I get all the events in my database:
$sql = "SELECT * FROM calendar WHERE date <= CURDATE() order by `date`";
This is my database data. In my opinion, all data are in the future...
The format of the date table is a default date-type:
When I ask my server about the time echo date("Y-m-d"); I get todays date as result...
So where do I make a mistake?
You may be checking the wrong date field. Do you have a created date as well as a scheduled date?
I could be crazy from the cold medicine I am on at the moment, but your date table can't possibly be the date of your calendar items, the id filed is only an int(2), that seems kind of small.
maybe something simplier? I notice the column name in your table is date, which also is the name of a function date() that returns the date part of a datetime value. If thats the case
$sql = "SELECT * FROM calendar c WHERE c.`date` <= CURDATE() order by `date`";
would do the trick. Even if not mysql itself, the gui app youre using (seems like phpmyadmin to me) might get confused.
(btw, you forgot the closing tick of date in the order by clause)
getting an empty set is meaning nothing is found matching. I would look at your formatting of your date. The only other thing i was thinking is that it is comparing an unmatched type so just returns an empty set.
use DATEDIFF :
DATEDIFF
WHERE DATEDIFF(date, CURDATE) > 0
Before you make your query, run this one:
SET time_zone = '-2:00'; // or whatever your time zone is.
Don't ask me how or why, but I've truncated my table and re-inserted some data and my query seems to work just fine:
$sql = "SELECT * FROM `calendar` WHERE `date` >= CURDATE() order by `date`";
So, despite the fact the problems seems to be solved by truncating my table, I would like to know the answer to the why-question... Anyone can provide me with it?
I'm relatively a newbie, would appreciate help :)
I am looking to find entry(ies) from a mysql table which were created some time between now and a certain timestamp in the past. This time in the past is stored in a variable (say $timeinthepast, a few hours ago or yesterday, whatever). And the column 'timecreated' in the table is the timestamp of the creation of entry.
Would the following work? If not, what would?
Thanks!
<?php
$query = mysql_query("SELECT * FROM table1
WHERE timecreated = DATE_ADD(NOW(), INTERVAL -$timeinthepast)");
?>
I am basing this on: Query to select records from a database that were created within the last 24 hours
As long as $timeinthepast is valid (1 MONTH, 2 HOUR, e.g.) in the sql you can try BETWEEN
$query = mysql_query("SELECT * FROM table1
WHERE timecreated BETWEEN
DATE_SUB(NOW(), INTERVAL $timeinthepast) AND NOW()");
I find that the between syntax is the most freindly to use. I have not tested this sorry :)
Select * from `table1` where `timecreated` between DATE_SUB(NOW(), INTERVAL 1 month) and NOW()
You will need to make your time in the past a whole unit of some description.
On a previous question about how I could limit the amount of times a form is submitted in an hour, someone said this:
select count(*) from mysql_table where uid='$uid' and timestamp > (DATE_ADD(now(), INTERVAL -1 HOUR););
That seem's like it would work, but I have no idea how. First I'd like to replace uid with IP:
select count(*) from mysql_table where ip='$ip' and timestamp > (DATE_ADD(now(), INTERVAL -1 HOUR););
But after that I'm not quite sure how the timestamp > (DATE_ADD(now(), INTERVAL - 1 HOUR);); actually works. I do submit a timestamp with each post, but I don't know how the rest actually works, can someone explain it to me?
Your query counts how many records are present in "mysql_table"
select count(*) from mysql_table
and limit that count to records having ip=user_ip
where ip='$ip'
and timestamp (the date/time column in which recording time is stored) is greater than time representing "one hour ago"
timestamp > (DATE_ADD(now(), INTERVAL -1 HOUR)
(remember that every time you insert a record, you probably set timestamp field to current date/time).
What you have to do is comparing this result with maximum allowed and decide if it's good or not.
I have the following query:
$yesterday=date("Y-m-d",mktime(0,0,0,date("m"),(date("d")-1),date("Y")));
$sql = "SELECT messages FROM user WHERE sent_date='".$yesterday."'";
With it I select all the messages sent to the users yesterday.
In my database sent_date looks like: 2010-12-28 11:55:30
But with $yesterday I get: 2010-12-28
So I get 0 results.
How can I modify my query so I can select all the messages from yesterday no matter the hour?
Thanks a ton
You should use MySQL Date functions for that (assuming the column is a datetime).
SELECT messages FROM user WHERE sent_date = DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
or always from 00:00
SELECT messages FROM user WHERE sent_date = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY);
If this is not giving any results, please edit your question with more info. You exact schema, how do you insert data and ensure that there are valid rows inside the table.
Perhaps by using
$sql = "SELECT messages FROM user WHERE date(sent_date)='".$yesterday."'";
That way you don't use the time in the datetime for comparison.
You could do a BETWEEN and put in a starting and ending time concatenated on the $yesterday...
Actually... what the next post suggests is better if you want the full date.