I have a user table, where users need to be approved, i want to show users who are not approved and is registered more than 7 days ago.
My user_regdate is a timestamp created with php time() function.
This is what i try, it does not works:
mysql_query("select * from users WHERE user_regdate < now() - interval 7 day AND approved='0' order by id;");
Thanks
PHP's timstamps are a simple integer, whereas MySQL's now() returns a datetime value. Most likely this will fix up the query:
SELECT ... WHERE user_regdate < unix_timestamp(now() - interval 7 day)) ...
Basically, without the unix_timstamp() call, you're comparing apples and oranges.
Primitive solution at best, but im not the best at MySQL time calculation
$timestamp = strtotime("-7 days");
mysql_query("SELECT * FROM users WHERE user_regdate < $timestamp AND approved = 0 ORDER BY id");
php's time() function outputs a unix timestamp (the number of seconds since January 1 1970). MySQL's now() function outputs a formatted date (like 2011-6-9 12:45:34)... so I don't think you can compare them like that.
Try using the unix timestamp, minus 7 days, instead of now() in your query:
$7_days_ago = time() - (7 * 24 * 60 * 60);
mysql_query("select * from users WHERE user_regdate <" . $7_days_ago . " AND approved='0' order by id;");
Try this;
select *
from users
WHERE DATE_SUB(user_regdate,INTERVAL 7 DAY)
AND approved='0'
order by id;
Related
I have a varchar field contain a timestamp value, i want to get the tomorrow records.
This is my code as you can see:
$tomorrow = strtotime("1+ day");
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow
but the query is incorrect
thanks for the help in advance
Should be like this :
$tomorrow = strtotime("+1 day");
/* this will select all record before tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date < $tomorrow;
/* this will select all record after tomorrow*/
SELECT * FROM tasks WHERE task_accomplish_date > $tomorrow;
http://php.net/manual/en/function.strtotime.php
SELECT * FROM tasks WHERE task_accomplish_date > NOW() + INTERVAL 1 DAY
You should be able to do this all in the MySQL query
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) = DATE(NOW() + INTERVAL 1 DAY)
That converts your task_accomplish_date into a date value and looks for records where that date is equal to today + 1 day (tomorrow). That does not give you records from 2 days from today or beyond, just tomorrow. If you want tomorrow and all records beyond tomorrow you would use
SELECT *
FROM `tasks`
WHERE DATE(`task_accomplish_date`) >= DATE(NOW() + INTERVAL 1 DAY)
If you are storing a timestamp, these only care about the date part of the timestamp, not about the time part of the timestamp. If you care about the time part as well you can remove DATE() from both sides of the = in the WHERE clause
I have users in a MySQL database, where their birthdays are stored in a DATE format (yyyy-mm-dd). I want to select users in the database with PHP who are between a certain age range.
What I have is a minimum age(18) and a maximum age(21). I know I can do something with BETWEEN, but the problem is that I only know the years and not the dates.
Does anyone have a suggestion on how I can do this?
This is what I'm doing currently:
function leeftijden($age) {
$morgen['day'] = date('d');
$morgen['month'] = date('m');
$morgen['year'] = date('Y') - $age;
$datum = $morgen['year'] .'-' .$morgen['month'].'-' .$morgen['day'];
return $datum;
}
Then I do this:
$maxDatum = leeftijden(18);
$minDatum = leeftijden(32);
$sqlRijder = "SELECT * FROM rijder WHERE geboortedatum between '".$minDatum."' AND '".$maxDatum."'";
But this doesn't work 100%.
How can I only select users who are between 18 and 21 years of age?
You can simply do it right in the query:
SELECT *
FROM rijder
WHERE geboortedatum BETWEEN
CURDATE() - INTERVAL 21 YEAR AND
CURDATE() - INTERVAL 18 YEAR
This way, you do not need a special PHP function to construct a DATE string. Simply pass in the numbers and MySQL will make the comparisons for you. Just make sure the higher number comes first in the BETWEEN.
Try this :::
Select * from table where (DATEDIFF(dob, DATE(NOW())) / 365.25) between 18 and 21
SELECT * FROM rijder
WHERE geboortedatum
between date_add(curdate(), interval -18 year)
and date_add(curdate(), interval -21 year)
try this
SELECT * FROM rijder WHERE DATEDIFF(NOW(), geboortedatum)/365.25 BETWEEN 18 AND 21
I have a table with the following fields:
id - int
name - int
the_date - int
the_date is a unix timestamp for when the row was added
Now I am trying to write a query that will select all rows on the day that is 7 days from now. I'm not talking about select the rows >= 7 days from time(), I need to grab the current day using time(), and then run a SELECT that grabs all the rows that were inserted on the day that is 7 days from the time().
I know how to do it so its within 7 days from the time() with a simple >= SELECT, but I don't know how to do it so it selects all rows whose unix timestamp is on that particular day (7 days from now).
Any help would be greatly appreciated.
The points of intrest here,
i use curdate() to get the current DATE, not DATETIME.
i add 7 days and convert to unix time, which yields the starting second of that day
i do the same for the next day
i structure the where clause to be >= or equal to the target day, but < the start of the next day. This gives a range of seconds that fully covers the target day.
i dont use any functions on the column itself. This is important because if i did, mysql wouldn't be abl;e to use any indexes that exist on the column to fullfill the query.
where the_date >= unix_timestamp(curdate() + interval 7 day)
and the_date < unix_timestamp(curdate() + interval 8 day)
SELECT * FROM `dbname1`.`test`
WHERE
date(FROM_UNIXTIME(`the_date`)) = ADDDATE(DATE(NOW()), INTERVAL 7 DAY);
$todayParts = getdate();
$startToday = mktime(0, 0, 0, $todayParts['mon'], $todayParts['mday'], $todayParts['year']);
$sevebDaysFromNow = 60 * 60 * 24 * 7;
$seventhStart = $startToday + $sevebDaysFromNow;
$seventhEnd = $seventhStart + 60 * 60 * 24;
$sql = "SELECT * FROM <table> WHERE the_date BETWEEN $seventhStart AND $seventEnd";
This will calculate 7 days from the start of the day you are now. Hope this helps
I have this;
$long = "86400";
$query = "SELECT * FROM users WHERE unixdate = UNIX_TIMESTAMP()-$long
ORDER BY unixdate DESC";
But it doesn't work. I would like to show all new users within 24 hours
You can do that query completely in MySQL with
SELECT col1, col2, otherCols
FROM yourTable
WHERE timestamp_col > (NOW() - INTERVAL 24 HOUR)
The expression (NOW() - INTERVAL 24 HOUR) returns the date 24 hours ago. MySql is smart enough to handle comparisons between Time related column types.
If timestamp_col is not a time related type, but something like a varchar or int column you have to use FROM_UNIXTIME on the column or adjust the above query to read
SELECT col1, col2, otherCols
FROM yourTable
WHERE timestamp_col > UNIX_TIMESTAMP( NOW() - INTERVAL 24 HOUR )
See DATE_SUB and DATE_ADD in the MySql Manual.
Use > instead of =. At the moment, you are querying for entries created at a certain second which will hardly ever match.
You're looking for new users within the last 24h, not exactly 24h. So you have to use the > (greater than) operator instead of = (equals).
$long = "86400";
$query = "SELECT * FROM users WHERE unixdate > UNIX_TIMESTAMP()-$long ORDER BY unixdate DESC";
By the way, PHP has a function equivalent to MySQL UNIX_TIMESTAMP() function: time();
You must convert the timestamp file to date for the comparison.
SELECT * FROM table WHERE (from_unixtime(unixdate) >= NOW() - INTERVAL 1 DAY)
I have a list of unix timestamps in a database, and I wanting to select the ones that are from today.
i.e If today is Tueday, I want to get all the timestamps that were made today? Is it possible? Is there such a things as strtotime("Today")?
Any help would be great
you can use mktime() to generate the timestamp for the start of the day and then find the database entries with a timestamp greater than that.
$start = strtotime(date('Y-m-d 00:00:00')); // Current date, at midnight
$end = strtotime(date('Y-m-d 23:59:59')); // Current date, at 11:59:59 PM
then, you can just select where the timestamp is between the above 2 timestamps:
"SELECT FROM `foo` WHERE `timestamp` BETWEEN '{$start}' and '{$end}'"
You can convert the unix timestamps to sql dates in the SQL using FROM_UNIXTIME(), then compare those to NOW()
SELECT * FROM `tablename` WHERE DATE(FROM_UNIXTIME(`dateFld`)) = DATE(NOW());
Check if DAY(NOW()) and MONTH(NOW()) and YEAR(NOW()) is equal to appropriate value of DAY(timestamp) and MONTH(timestamp) and YEAR(timestamp).
select timestamp from table where DAY(NOW()) = DAY(timestamp) AND MONTH(NOW()) = MONTH(timestamp) AND YEAR(NOW()) = YEAR(timestamp)
If you're using mysql:
SELECT * FROM table WHERE DATE(NOW()) = DATE(FROM_UNIXTIME(timestampcol))
FROM_UNIXTIME(somefield) can be compared to CURDATE() assuming you're using MySQL
SELECT * FROM mytable WHERE FROM_UNIXTIME(datefield,'%Y-%m-%d') = CURDATE();
ETA:
Okay, I was assailed by doubt when this answer was marked down. So I went and did a couple of tests. Given MySQL it definitely works. So why the downmod?
Consider this test which outputs 2 identical fields for every row in a table:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d') a , CURDATE() b
FROM tablewithsomerows
WHERE FROM_UNIXTIME(UNIX_TIMESTAMP(CURDATE()),'%Y-%m-%d') = CURDATE();