I have a column in MYSQL that stores the datetime like so:
2017-08-18 08:59:53
2017-08-18 07:59:00
I can search the database and return the results between the datetime column like so:
$from = date("Y-m-d H:i:s", strtotime($_GET['from']));
$to = date("Y-m-d H:i:s", strtotime($_GET['to']));
$sql1="SELECT * FROM `Table` WHERE mydate>='$to' and mydate<='$from'";
This works fine as long as I search like this:
2017-08-18 08:59:53
But what I need to be able to do is to search like this:
2017-08-18
and still get the same result when I search.
I don't want to use the time in my search string/query.
Try mysql date() function like:
$sql1="SELECT * FROM `Table` WHERE date(mydate)>='$to' and date(mydate)<='$from'";
mysql date() function returns only date part from datetime column in default Y-m-d format.
You are very close. If you want to search a DATETIME or other datestamp column for matches to a particular day, do this:
WHERE mydate >= DATE(whatever_first_day)
AND mydate < DATE(whatever_last_day) + INTERVAL 1 DAY
This searches for everything on the date mentioned in whatever_first_day, starting with midnight on that date, and everything up to, but not including (<), midnight on the day after whatever_last_day.
This way of searching is sargable: it can use an index on the mydate column.
You could also use
WHERE DATE(mydate) >= DATE(whatever_first_day)
AND DATE(mydate) <= DATE(whatever_last_day)
but that is not sargable.
"SELECT * FROM `Table` WHERE date_format(mydate,"%Y-%m-%d")>='$to' and date_format(mydate,"%Y-%m-%d")<='$from'";
Related
I have my datetime going into the DB like this:
$CurrentTime = time();
$DateTime = strftime("%b-%d-%Y %H: %M: %S", $CurrentTime);
In phpMyAdmin it looks like this:
May-15-2018 01: 04: 00
Feb-08-2018 13: 49: 23
etc...
The field is varchar(50)
I'm trying to extract posts from this table based on the month that the post was created.
I have tried the following:
"SELECT * FROM admin_panel WHERE MONTH(datetime) ='2'"
"SELECT * FROM admin_panel WHERE MONTH(STR_TO_DATE(datetime, '%b-%d-%Y %H: %M: %S')) ='2'"
I don't get an error, but nothing is returning. Any ideas on how I can correct this?
You can use MYSQL STR_TO_DATE with MONTH
"SELECT * FROM admin_panel WHERE MONTH(STR_TO_DATE(`datetime`,'%b-%e-%Y %H:%i:%s')) = 5"
Adding Year with AND condition
SELECT * FROM admin_panel WHERE MONTH(STR_TO_DATE(`datetime`,'%b-%e-%Y %H:%i:%s')) = 5 AND YEAR(STR_TO_DATE(`datetime`,'%b-%e-%Y %H:%i:%s')) = 2018
The following works when the 'datetime' column is defined as DATETIME, yyyy-mm-dd h:i:s (2018-06-14 14:29:15) and not as VARCHAR,
SELECT * FROM admin_panel where extract(month from datetime)='02'
I think, if you use a VARCHAR instead of a DATE.. type, then you will have to typecast string to a date for performing any date-based functions on it in a query. Imo, it is better to have date stored in DATE.. format and let MySQL store it in some format; but you could format it using DATE_FORMAT functions for displaying.
When column type is DATETIME, month() call also can be used as in,
SELECT * FROM admin_panel WHERE month(datetime)='02'
I have a table containing a field "created_at" whose Data Type is timestamp. I donot want to change it to DATE.
Can i query this table to fetch all rows of a day in format dd-mm-yyyy.
Note:
One approach I tried is:
a) Take Date in yyyy-mm-dd concatenate with 00:00:00
b) Take next date in yyyy-mm-dd concatenate with 00:00:00
and use below query to get all records of that day:
SELECT *
FROM news
WHERE created>='2016-12-13 00:00:00'AND
created < '2016-12-14 00:00:00'
Is this a good solution to my problem. Any better approach for this problem.
You can use the MySQL cast() function.
SELECT
*
FROM news
WHERE CAST(created_at AS DATE) = '2016-12-13'
This will discard the time component of your timestamp and do the comparison on only the date.
Reference: http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html
If you don't want to change column datatype then you can go with this code
$startDate = strtotime( '2016-12-13' ); // it will convert date to timestamp format.
$endDate = strtotime( '2016-12-15');
$query = "SELECT * FROM news WHERE created >= '$startDate' AND created < '$endDate'";
I'm truly stumped on something - I have a table in my database with a column called 'today' that has Date and Time records. The column has entries that look like this:
October 25, 2014, 4:58 am
October 25, 2014, 4:36 am
I'm having trouble pulling the rows by date; I think the time stamp is messing with the MySQL query. And I need an SQL query to pull any records where the variable $today matches the date information in the column 'today'. This doesn't work:
$today = date("F j, Y"); // looks like this: October 25, 2014
$result = mysqli_query($link,"SELECT * FROM records WHERE today = $today"); // 'today' represents the column in the table
while($row = mysqli_fetch_array($result)) {
echo var_dump($row);
}
I just get an empty result, I think due to the time stamp. Can someone advise on a better MySQL query that will only grab the rows where $today matches the date in the 'today' column?
Although storing the date and time as string in varchar is not really a good idea, you could still alter your query to match string containing the current date with a LIKE statement:
$result = mysqli_query($link,"SELECT * FROM records WHERE today LIKE '$today%'");
That is just to get your current setup working as a temporary fix but i highly suggest you take a look at datetime and timestamp or similar date types if this is a serious project and not just playing around. with programming.
UPDATE
With a datetime you could get the dates which are the same as today with:
SELECT * FROM `records` WHERE `today` = CURDATE();
with a timestamp you would need to pass it as date so your query would be:
SELECT * FROM `records` WHERE date(`today`) = CURDATE();
You can just use the MySQL date functions:
SELECT *
FROM records
WHERE today = CURRENT_DATE;
If there is a time component on the today column, then the best structure is:
SELECT *
FROM records
WHERE today >= CURRENT_DATE and today < date_add(CURRENT_DATE, interval 1 day)
It's obvious that both dates are not equal. Both dates are treated like text values and are not equal. You need to convert the column containing date in your MySQL query as such:
$result = mysqli_query($link,"SELECT * FROM records WHERE DATE_FORMAT(today, '%F %j, %Y') = $today");
Note that you have to change your column to store values of the type of DATE. Or just use queries as proposed in other answers.
I'm working with a table and there is field in my table which stores raw time() function value as date.
I want to get rows with today date from this table .
So i figure out when time() func returns a 10 digit number like 1316352184 the first 5 digits are for year , month , day which i need for getting today's date and the rest is for hour minute Second which i dont need
So i get today without hour and... like
$t = time();
$t = $t /100000;
$today =(int)$t;
Now i need to get rows with today date from the table but i'm not sure how to do that.
How can i get first 5 digits of stored date in database in my query to compare it with $date?
Something like this:
$sql = "SELECT * FROM TABLE WHERE ((int)date/100000) as date = $today ;
select * from table
where from_unixtime(unix_timestamp_field,'%Y-%m-%d') = curdate()
Why you don't use:
$sql = "SELECT * FROM TABLE WHERE date(date) = date(NOW());
What you have is a UNIX timestamp. The number of seconds since January 1st, 1970.
You can use date() and mktime() to work out what todays timestamp is, then do date > the timestamp. If that make sense.
Sounds like you should use the DATETIME or TIMESTAMP data type for your column so you can use MySQL's date functions.
Would this be possible? I've used this to insert the date into a field called "date":
$date=date("m/d/y");
$sql="INSERT INTO pool (date) VALUES('$date' )";
$result=mysql_query($sql);
I've used this statement to get the date a week ago:
$variable = date('d-m-y', strtotime('-1 week'));
So how would I SELECT any rows which were added last week?
Instead of storing your dates as m/d/y, you should store them as Y-m-d :
$date=date("Y-m-d");
$sql="INSERT INTO pool (date) VALUES('$date' )";
In the database, you dates will then look like 2011-04-09.
That format is much easier to work with : alphabetical comparisons will work.
Which means that searching for rows that are older than a certain date would become something like this :
$variable = date('Y-m-d', strtotime('-1 week'));
$query = "select * from pool where date < '$variable'";
Also note that instead of working with a date field which is a varchar (or an equivalent) in your database, you could use a DATE column -- which would allow to to work with date and time functions in MySQL.
If the date field is a proper date type you can do < or > in your sql query. For example -
SELECT * FROM table WHERE date > '$date'
If you want everything from 1 week ago to now you can do something like the above or
SELECT * FROM table WHERE date BETWEEN '$date' AND NOW()