PHP Select all from current and future dates only - php

I need to list and records in a table where "theDate" field is either current or future.
The "theDate" field will contain a date in this format: Year-month-day ... so something like: 2014-12-31
So I have SELECT * FROM events but I need the WHERE part so I can get only current and future dates.
How can I do this?

You can use a MySQL keyword called NOW() and a simple greater than or equal to test
WHERE theDate >= NOW()

SELECT * FROM events
WHERE date_column >= CURDATE();
date_column being the name of your column that contains the date.
CURDATE() returns the current date.

Related

PHP count mysql row with changed date format

I have next problem:
My table date format was: LIKE 2017-01-08 18:50:25 (with time).
When i use sql query like
'SELECT date FROM table WHERE date = "2017-01-08"'
My row was empty, i need COUNT all row with same (today) date WITHOUT TIME.
Note, i will not change INSERT date time!
Use DATE() to get the date portion of the datetime field and compare it to today. Use COUNT() to get the number of records that match your query.
SELECT count(*) FROM table WHERE DATE(date) = CURDATE()
You can also replace CURDATE() with NOW(), CURRENT_DATE(), and CURRENT_DATE
You can also use it in the following way
'SELECT date FROM table WHERE date_format(date,'%Y-%m-%d') = "2017-01-08"'
the date_format is mysql function which return date according to your pattern the above pattern only return the Y-m-d from the datetime
I hope it will help you
plz change your statement equal operator to greater than
'SELECT date FROM table WHERE date > "2017-01-08"'
as by default if time portion is not present then it is putting 00:00...

Select items between two dates, varchar format

I have a table who contains a field 'date' which is a date in varchar format ("2016-07-30"). I would like to select all items which are between the date of today and the date of today +1 month. I already done the variable who contains the two dates (in date format) but I don't know how to select items between these two dates because the field of my table is in varchar and not in date format.
Thank you so much for your help.
Try something like this:
select *
from yourtable
where str_to_date(datecol, '%Y-%m-%d') between now() and date_add(now(), interval 1 month)
But the right thing you should do is alter your column type to date.

Order mysql query by date and time which are separate columns

I wish to order my mySQL query by date and time as if they were a combine column (datetime) however they are a separate column (Date and Time). I need to be able to display all the records from a table that are after the current time (using Date("")).
If we assume you have table t with structure:
id | date | time
Then to order by date and time you do:
SELECT * FROM t ORDER BY date,time
To select everything that's newer than the current time do:
SELECT * FROM T WHERE date > CURDATE() OR (date = CURDATE() AND time > CURTIME())
There's an alternative way to do the second one which is based on Creating DATETIME from DATE and TIME so you can refer to that if you need to.
You can combine the above and do a :
SELECT * FROM T WHERE date > CURDATE() OR (date = CURDATE() AND time > CURTIME()) ORDER BY date,time

How to match current month from timestamp field?

I have a field named modified which value is timestamp. i.e: 2015-11-10 07:42:56
I want to match current month only, e.g.
SELECT * FROM table WHERE modified = 2015-11
I will replace 2015-11 by current month. But how can I compare only yyyy-mm part from this timestamp value?
You can use YEAR and MONTH
SELECT * FROM table WHERE YEAR(modified) = '2015' AND MONTH(modified) = '11'
If the modified column is indexed, do something like below to get better performance:
SELECT * FROM tablename
WHERE modified >= '2015-11-01' and modified < '2015-12-01'
Note: I'm not quite sure about these date literals. (Not a MySQL user.)

Getting wrong answer with mysql date query

Here is my table
I am executing a query that give me result of fields whose item_valid_from must be greater than today's date and item_valid_to must be less than today.
My query is
select *
from tbl1
where item_valid_from >= CurDate()
and item_valid_to < CurDate()
Any Solution?
I would advise you to change item_valid_* field formats to DATE field format. You will save you a lot of trouble in the future.
But ok, if you don't want to do that, then you can use STR_TO_DATE() function:
SELECT *
FROM `table`
WHERE CURDATE() BETWEEN STR_TO_DATE(`from_field`, '%d-%m-%Y') AND STR_TO_DATE(`to_field`, '%d-%m-%Y')
demo
Assuming the datatype item_valid_from and item_valid_to is DATE, TIMESTAMP, etc, then you have your operators backwards. Think of the time as seconds since 1970, since this is how it is stored in unix time. That means that item_valid_from is going to be smaller than item_valid_to, and you want it to display when today is somewhere between them. You want the item_valid_from to be less than or equal to now, and the item_valid_to to be greater than now (not in the past).
SELECT *
FROM tbl1
WHERE item_valid_from <= CURDATE() AND item_valid_to > CURDATE()
See this SQL Fiddle for an example, only 2-4 are valid and show up in the results being valid from a date in the past and expiring on a date in the future.
You have to use following query which change current date format then compare date and fetch result :
SELECT *
FROM tbl1
WHERE date_format(item_valid_from,'%d-%m-%Y') >= date_format(CurDate( ),'%d-%m-%Y')
AND date_format(item_valid_to,'%d-%m-%Y') < date_format(CurDate( ),'%d-%m-%Y')
Please Check this :http://sqlfiddle.com/#!2/561d0/2

Categories