I have a date, say its called $date. I want a a mysql_query to search a select number of weeks,days or even months before my $date. Is this possible? My explanation is not the greatest, but I do need a answer for this and do not know how to properly question it.
You could use mysql interval function?
"select * from table where `date` BETWEEN DATE_SUB(".$date.",INTERVAL 15 DAY ) AND CURDATE( )
That'll return the records from the last 15 days, you could use = insted of between if you want the records exactly 15 days old, or modify it for days, months, etc.
edit: if your working with php's time() remeber to use FROM_UNIXTIME($phpdate) inside your query.
i have a solution for this in SQL, Take it, if it would helps you
Day($date) gives you the date in the vaariable
Month($date) gives you the Month in the vaariable
Year($date) gives you the year in the vaariable
using simple where conditions, now you can search for a particulars
You can use the DATE_ADD and DATE_SUB functions to modify a date, and mysql understands a BETWEEN clause using dates. However, you can also use the TIMESTAMPDIFF function like so:
"SELECT foo FROM table WHERE TIMESTAMPDIFF(DAY, dateField, '$date') < '$desired_days'"
Related
I have a question. So I need to get data from the last days of months using a sql request. For example :
Now date is 22/08/2016, I need to get data from 01/08/2016-22/08/2016;
Now date is 04/06/2016, I need to get data from 01/06/2016-04/06/2016;
Sorry but I dont have an idea. Thx in advance and sorry for my english
I would do something like this:
SELECT <table-columns> FROM <table-name>
WHERE (<date-column> BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
This DATE_FORMAT(NOW() ,'%Y-%m-01') will return the first date of your current month and year.
Need to use something like:
YEAR(time) = YEAR(NOW())
AND MONTH(time) = MONTH(NOW())
You are looking for something like this:
SELECT * FROM tbl_name WHERE yourDate.MONTH()=NOW().MONTH();
Refine the syntax from here.
You need to get the first day of the month for a given date then, in order to make your comparison with the database:
select date(concat_ws('-',year(curdate()),lpad(month(curdate()),2,'00'),'01')); gives you that (for curdate() here)
I'm trying to do a SELECT * FROM but only items that are less than 30 days old. Here is my select code:
SELECT * FROM `{$table_name33}` WHERE `type`='wpst-requiredinfo' ORDER BY `foreignkey` ASC;
However, my problem is that I can't figure out how to add WHERE AND last_updated is less than 30 days.
I'm not exactly sure how to write the query, but the date is showing up like this: 1428412603 in the table column, it doesn't look much like a date to me. I don't know where to start.
Try this where clause:
WHERE `type`='wpst-requiredinfo' and
last_updated >= date_sub(now(), interval 30 day)
EDIT:
Your date seems to be in Unix time format.
WHERE `type`='wpst-requiredinfo' and
last_updated >= unixtime_timestamp() - 30*24*60*60
Note: this puts all the functions on the current time. In particular, it does not use FROM_UNIXTIME(last_updated). This ensures that an index can be used for this part of the query. The best index would be on (type, last_updated).
Although I have been working with PHP for a while, the one part of it I am still trying to get right is time.
I am creating a simple script that will check if the timestamp is greater than or equal to an hour, and if it is, it will be deleted from the database.
2013-01-03 20:30:25
DELETE FROM tablename WHERE timestamp = ?????
I am not sure how to execute the query to delete values with a timestamp of over an hour from the current time. Any help is greatly appreciated.
DELETE FROM tablename WHERE `timestmap` < DATE_SUB(NOW(), INTERVAL 1 HOUR)
Ref:- date_add and date_sub
First of all, 2013-01-03 20:30:25 is not a timestamp, it is a formatted date. The timestamp for that date would look like this: 1357245025. You can convert it to a timestamp using the strtotime function. You can also work out the timestamp of an hour ago by using strtotime("-1 hour") and performing a comparison on the values.
It might be faster just to do all of this within the MySQL query though, MySQL provides queries to do this, using a query similar to the one that Amit Garg provided.
Is there a way to compare a datetime field in mySQL with the current date and return the time difference? I don't really know where to start so if anyone has an idea, please point me to the right direction.
Here's what I already tried.
SELECT *
FROM `order`
WHERE `dateTimeAdded` - NOW( ) <1;
basically what I am trying to achieve is to get the orders which is saved for more than 8 hours in the database. Thanks!
If you want your query to have a chance of using an index on the datetime column, you should not do manipulation (functions) on the column but only on the other side:
SELECT *
FROM `order`
WHERE dateTimeAdded < NOW( ) - INTERVAL 8 HOUR ;
If u just need the diff of dates use
DATEDIFF(now(), column)
and if you need the time difference use
TIMEDIFF(now(),column)
DATEDIFF(date1,date2);
Taken FROM
I'm trying to request any records from a table that appear on or after today's date, using a single query.
All the dates for each record are stored in separate columns e.g.. - one for month, one for year, and one for day.
obviously i've been getting all records that occur after the year in today's date
$sql = "SELECT * FROM table WHERE year>=".$year_today."
ORDER BY year, month, day";
Then i've been trying to filter that down a bit, by using:
$sql = "SELECT * FROM table
WHERE year>=".$year_today." && month>=".$month_today."
&& day>=".$day_today."
ORDER BY year, month, day";
And in order to test it, i created a record in the database with yesterdays date, yet, this record still appears in the list of returned records. What am i doing wrong? :(
This can be achieved using time functions in a nice way, while it's better when using a DATE column.
SELECT * FROM TABLE WHERE TIMESTAMP(CONCAT(year,"-",month,"-",day)) >= CURDATE()
ok, that's evil as it doesn't use an index ... proper thing would be a DATE column, but doing all this by hand is annoying asyou also have to consider the case where year is bigger but months is smaller and stuff ....
I don't know if you can use "&&" instead of "AND" in your context - maybe try changing your SQL to use
" AND month>=".$month_today." AND day>=".$day_today."
EDIT :
as an extension to Nick's answer, to make the sql behave correctly you could code :
$sql = "SELECT * FROM table WHERE (
(year>".$year_today.")
OR (year=".$year_today." && month>".$month_today." )
OR (year=".$year_today." && month=".$month_today."&& day>=".$day_today.")
)
ORDER BY year, month, day";
...but this starts to get a whole lot messier than converting to dates and using date comparison
EDIT2:
but then again, if these columns are indexed, the indexes might be used. It's still a lot more effort to code SQL this way if performance using conversion to dates is totally acceptable.
To compare dates with dates:
SELECT * FROM table WHERE
STR_TO_DATE(
CONCAT(year
, '-'
, right(concat('00', month), 2)
, '-'
, right(concat('00', day), 2))
, '%Y-%m-%d')
>= STR_TO_DATE('2009-12-11', '%Y-%m-%d')
to check if a date is after another date this query will fail:
$sql = "SELECT * FROM table WHERE year>=".$year_today." && month>=".$month_today." && day>=".$day_today." ORDER BY year, month, day";
day 12 march of februari is smaller then day 15 in februari, but is still after it, because it's in a different month....