How to Fetch Data using Date Condition in php - php

Hello I am working with Word Press and I want to fetch records according to the the
date.MySQL database has a table wp_evr_event with end_date column which has
a number of dates in end_date column according to each record like:
1. 2017-1-4
2. 2017-1-6
3. 2017-10-10
I want to fetch those record which has End date greater than to the current
date.I used Query
SELECT * FROM `wp_evr_event` WHERE `end_date`>'2017-1-31'
But I get number of records which has the less date to the current date.
How to resolved this problem.

You need to use DATE() of mysql like below:-
SELECT * FROM `wp_evr_event` WHERE DATE(`end_date`)>'2017-1-31'
Note:- I think that end_date field is of datetime type , so that's why your code is not working

You need to use str_to_date MySQL function to convert string into date. Here's the documentation.
Your query will look like this:
SELECT * FROM wp_evr_event
WHERE str_to_date(date, '%Y-%m-%d') > '2017-1-31';
Here's the SQL Fiddle.

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...

How to show rows from mySQL from a particular date using PHP?

In mySQL I have a timestamp column named when
2015-01-07 16:43:21
My question is how using PHP/mySQL
For now I can show the results based on month number like
... where month(`when`) = '1' ...
but what if I want to show the rows of a particular date for example 2015-01-05 ?
I will pass the preferable date through a variable into the sql query.
Just wrap the date() function around your date string which is already in the YYYY-MM-DD format, and also around your timestamp field in the where clause:
select * from table where date(`when`) = date('2015-01-05');
From the documentation for date():
Extracts the date part of the date or datetime expression expr.
Here's how to convert the date via MySQL:
SELECT
DATE_FORMAT(`when`, '%Y-%m-%d') AS my_date
FROM
my_table
WHERE
MONTH(`when`) = 1;
Take a look at the MySQL documentation here for other formats: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

Selecting all timestamps for a certain date in PHP

I have a table with a few records and for each of these records I've also added a UNIX_TIMESTAMP. Now, I also have a search engine for those records in which I can choose a date using a jQuery datapicker. My question is how do I make the request so that to select all timestamps from the database for a certain date.
With an index on your timestamp column you will get a faster result with:
SELECT *
FROM table_name
WHERE time_stamp_column_name >= :date_picked
AND time_stamp_column_name < :date_picked + INTERVAL 1 DAY
Where :date_picked is your bound-in picked date as a string in the format 'YYYY-MM-DD'.
You can use from_unixtime to convert it into a date
select *
from
table
where
date(from_unixtime(your_timestamp_col)) = '2014-10-01'
use unix timestamp
you can also see this

Wrong date from mysql in order by

Greetings hope to get some help from you here as i been searching high and low for this.
This query works but its not the results i wanted more correctly not the right date format i want it in.
SELECT DISTINCT colum FROM table WHERE colum IS NOT NULL
This query gives me the dates
01.04.13
02.04.13
03.04.13
30.03.13
31.03.13
I wanted it to show latest date.
This info is posted in colum that stores the info as text. It is posted in the following format
dd.mm.yy
I wanted it then to show me the results as in
03.04.13 since this is todays date. i know i can limit it to 1 but still it will show the wrong date.
Thank you again for all help so far
First, you should not be storing a date as a string, you should store a data as a DateTime datatype.
Since you are storing it as a string, you will have to convert it to a date to get the max() date value. The following uses the STR_TO_DATE() function to convert the string to a date to get the max value:
select max(str_to_date(yourdate, '%d.%m.%y')) MaxDate
from table1
See SQL Fiddle with Demo
Try
select distinct column from tab where column1 = date_format(curdate(),'%d.%m.%y') AND brukernavn is not null order by brukernavn;
1) You need to save DATES in date or datetime
2) try MAX() function in MySQL
or try to ORDER BY date DESC and LIMIT 1
3) by the way, did you try using DISTINCT with GROUP BY?

php and mysql use the date range can not show

I had the question for the mysql date between select. In the mysql table, the field is varchar. The date range is '21-01-2013' and '31-01-2013', it can show the records, but the date range is '21-01-2013' and '20-02-2013', it cannot show the records.
"SELECT * from away_from_office where (awaydatefrom between '21-01-2013' and '31-01-2013') ";
you should convert it to date first using STR_TO_DATE, eg.
SELECT *
from away_from_office
where awaydatefrom between STR_TO_DATE('21-01-2013', '%d-%m-%Y') and
STR_TO_DATE('31-01-2013', '%d-%m-%Y')
if the column has the same format with the one you've shown, convert it also
WHERE STR_TO_DATE(awaydatefrom, '%d-%m-%Y') BETWEEN
if you have a chance to alter the table, or you're working with sample records, alter your table by changing the data type of the column to DATE.
Change column datatype to DATE
http://dev.mysql.com/doc/refman/5.1/en/datetime.html

Categories