select timestamp older than - php

How i can select from database the items older than 12 hours?!
I using a timestamp column to store the time but I don't think i need year,month,day only hours
I have something like this but it dosen't work (no error just returning all data from table)
$sql = "SELECT *FROM Y WHERE X and time > now() - INTERVAL 12 HOUR";
Type : timestamp
You're right Salman A .Thanks

Try this :
SELECT * FROM Y WHERE X and time < (NOW() - INTERVAL 12 HOUR)
you need < rather than > as you want to select records older than 12 hours

Related

Sql query to get row from date filled

I want to get data from training_course table where current date minus 5 days is equal to training_end_date. Training_end_date is my field in the table.
Thanks
You seem to want:
select *
from training_course
where training_end_date = current_date - interval 5 day
Or, if your dates have time components, you maybe want:
select *
from training_course
where training_end_date >= current_date - interval 5 day and training_date < current_date - interval 4 day

mysql group_concat showing all dates

I have 2 tables:
profile userid, fname,lname
schedule: userid, date, start_time, end_time
Here is my query .
select *, group_CONCAT(distinct(date),'_',start_time,'_',end_time)
as dateformat FROM profile, schedule WHERE profile.userid =
schedule.pid
This returns all dates from the table. But what I am trying to do is return only certain days like:
date >= DATE(NOW()) and date <= date(now() + 2 day)
So today tomorrow and the day after.
No matter what I try it just returns all dates in the schedule database. I need this to return 2 rows userid 5 has 1 row in profile but 5 rows in schedule and userid 6 has 1 row in profile and 2 rows in schedule the query works fine its just returning all dates and not the dates within the
"date >= DATE(NOW()) and date <= date(now() + 2 day)"
did you try GROUP BY?
like WHERE profile.userid = schedule.pid GROUP BY date
select profile.userid,schedule.userid, group_CONCAT(distinct(date),'_',start_time,'_',end_time) as dateformat FROM profile, schedule WHERE profile.userid = schedule.userid and date >= DATE(NOW()) and date <= date(now() + interval 2 day);
That did it for now!! Now I can use the dateformat variable in php to parse out my results.
kanishka panamaldeniya - thanks for your suggestion!

MySQL query to select records newer than x days

I'm sorry if this sounds like a very basic question but for some reason, today I'm really having trouble getting my head round this. I have a database table with a date_added column in the format of 2014-09-30 20:39:17 and I have a web page with filter options for users. Basically I want to use variables to select different date ranges like so:
SELECT * FROM table WHERE date_added = /* EVERYTHING POSTED TODAY */
SELECT * FROM table WHERE date_added = /* EVERYTHING POSTED WITHIN LAST 7 DAYS */
SELECT * FROM table WHERE date_added = /* EVERYTHING POSTED WITHIN LAST 30 DAYS */
What would I need to put in to get those variables to work?
You can use CURDATE() and very simple INTERVAL arithmetic.
In the following examples assume that query was executed at 2014-10-21 22:25:28:
SELECT * FROM table WHERE date_added >= CURDATE()
-- >= 2014-10-21 00:00:00
SELECT * FROM table WHERE date_added >= NOW() - INTERVAL 24 HOUR
-- >= 2014-10-20 22:25:28
SELECT * FROM table WHERE date_added >= CURDATE() - INTERVAL 7 DAY
-- >= 2014-10-14
SELECT * FROM table WHERE date_added >= CURDATE() - INTERVAL 30 DAY
-- >= 2014-09-21

selecting all rows 7 days from now based on unix timestamp and current date

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

Grouping datetime fields into chunks

I have a mysql MyIsam table with a field datestamp (datetime), I am trying to return all records that have a datestamp within the last 3 hours, but group it into 10 chunks (so for this example group one would be the first 18 minutes etc...)
Any ideas? Is this possible in mysql?
I think this may work:
$t = date("U",strtotime("-3 Hours"));
$sql = "SELECT *, FLOOR((UNIX_TIMESTAMP(datestamp) - ".$t.") / 1080) as chunk
FROM table WHERE UNIX_TIMESTAMP(datestamp) > '".$t."' GROUP BY chunk";
Devide the difference with 1/10th of the 3 hour interval (1080 sec). This gives chunks 0,1,2...9, where 0 means the first 18 minutes.
Sure. As I'm not really a MySQL expert, this is pseudocode:
SELECT ..., (now() - datestamp) AS age, age / 600 AS group, ...
Then you have two columns, "age" that you use to limit the selection, and "group" that further subdivides.
use like
SELECT SUM(myfields)..... FROM mytable
WHERE mydate >= DATE_SUB( NOW() , INTERVAL 3 HOUR)
GROUP BY FLOOR((UNIX_TIMESTAMP() - UNIX_TIMESTAMP(mydate) ) / 10800)
10800 is the number of second in 3 hours

Categories