php and mysql use the date range can not show - php

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

Related

getting data for specific months from date in mysql

I have a column in MySQL datatable named added_date, datatype of it is varchar(255) which is inserting date like mm/dd/yyyy. I have a lot of data in the table for dates like '5/12/2018', '4/10/2018', '3/5/2018' etc. Now, I want to get data for may, 2018 month only.
How to have the data for may, 2018 only?
Thanks.
MySQL retrieves and displays dates in 'YYYY-MM-DD' format. Thus, you have to use STR_TO_DATE function to convert string to date. Then it is possible to use BETWEEN keyword in WHERE clause to set specific date range:
SELECT *
FROM MyTable
WHERE STR_TO_DATE(added_date, '%m/%d/%Y') BETWEEN '2018-05-01' AND '2018-05-31';
You could try the query online.
Convert varchar to date within a format that is suitable for your condition:
SELECT * FROM your_table WHERE STR_TO_DATE(added_date, '%c%Y') = '52018';
You can use the same function in the select component to return the date too:
SELECT STR_TO_DATE(added_date, '%Y-%m-%d') AS fDate FROM your_table WHERE STR_TO_DATE(added_date, '%c%Y') = '52018';

How to Fetch Data using Date Condition in 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.

Sort by Date field which is a VARCHAR in the database

All,
I have the following query:
SELECT ...
FROM....
WHERE...
ORDER BY gear_checkout_summary.check_out_date DESC";
I then have the following dates for example:
12/29/2012
11/30/2012
09/04/2012
07/21/2013
07/08/2013
06/29/2013
They are sorted this way when my results are returned. My data element is stored as VARCHAR(10) in my database since I messed up that design. Is there a way to change my query so it sorts the dates correctly?
Thanks!
Try this:
ORDER BY SUBSTR(check_out_date, 7) DESC, check_out_date DESC
This first orders by the year using the SUBSTR() function. For rows that have the same year, it then orders by the date itself. This second-level ordering doesn't require SUBSTR() because MM/DD is already ordered properly.
STR_TO_DATE will see your job
STR_TO_DATE(gear_checkout_summary.check_out_date, '%c/%e/%Y') // will give '2013-06-23' in Date format
This will convert your varchar field into date format.
This would put the date in the proper MySQL date format and then ordering would be simple.
SELECT
STR_TO_DATE(gear_checkout_summary.check_out_date,"%m/%d/%Y") AS fixed_date
...
FROM ...
WHERE ...
ORDER BY fixed_date DESC
As others have stated, you should fix the format in the database if at all anyway possible. You may need to change the insert statements, but it will be worth the extra work. Here is what you would need to do to format the dates in the database
ALTER TABLE gear_checkout_summary ADD check_out_date_temp DATE AFTER check_out_date;
UPDATE gear_checkout_summary SET check_out_date_temp = STR_TO_DATE(gear_checkout_summary.check_out_date,"%m/%d/%Y");
ALTER TABLE gear_checkout_summary DROP check_out_date;
ALTER TABLE gear_checkout_summary CHANGE check_out_date_temp check_out_date DATE;
Here is explanation of above code:
Create new column in your table.
Populate the new column with the dates from the previous column formatted correctly.
Drop the old bad formatted column.
rename new column to previous columns name.
Use str_to_date:
select *
from your_table
order by STR_TO_DATE(yourDateField,'%m/%d/%Y') desc;
This will convert the string to a valid MySQL date. Check this link: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date
You should change your data type to DATE... it is the easiest and cleanest way to store dates.
I tried all codes and at the end I achieve my sorting on varchar date with this code...
ORDER BY SUBSTR(check_out_date, 7) DESC, ORDER BY SUBSTR(check_out_date, 1,2) DESC,ORDER BY SUBSTR(check_out_date, 4,2) DESC
First sort by year, then by month and at the end by day...

Wrong search between dates

In the database I have table users which have column 'birthday'
Here is the sql query that I use to fetch the results
SELECT *
FROM (`users`)
WHERE `birthday` between '12/11/1982' and '31/01/1983'
The problems is that I get records only for year 1982?
Notice that records for 1983 year exists.
What could be the problem here ?
This is just an alternative answer from Mark Byers. If the data type of your column birthday is varchar and has format of dd/MM/yyyy, please do read this; otherwise, read from Mark Byer's.
The first thing you need to do is to convert your column into DATE or DATETIME datatype by using STR_TO_DATE,
SELECT *
FROM `users`
WHERE STR_TO_DATE(`birthday`,'%d/%m/%Y')
between '1982-01-12' and '1983-12-31'
SQLFiddle Demo
SOURCES
STR_TO_DATE
DATE formats
You should put the year first in your date literals:
SELECT *
FROM `users`
WHERE `birthday` between '1982-11-12' AND '1983-01-31'
From the documentation:
Date and time values can be represented in several formats, such as quoted strings or as numbers, depending on the exact type of the value and other factors. For example, in contexts where MySQL expects a date, it interprets any of '2015-07-21', '20150721', and 20150721 as a date.
If you are using a varchar type then it still makes sense to put the date first (both in the data and in the query) because then your dates will sort correctly when using the default sorting order.

PHP mysql: how do I select records for today, or a particular day?

I am working with a table on which I can't change the structure.... Now there is a varchar column which contains a timestamp. Now I need to select the records whose timestamp translates to the current date, or a specified date.
Any help?
First off you shouldn't be storing date information in a mysql database with a VARCHAR field. Rather use DATETIME that is what it is for. I can only guess how you have stored your timestamp date in the database but I am going to assume it is the following format:
YYYY-mm-dd hh:mi:ss (ie '2011-04-15 09:23:55')
You now have to format your input which I am assuming is a time object or it is a string in the same format as the data in the database:
$yourdate = strftime("%Y-%m-%d", $input);
then construct your query
query = "select * from table where substring(datecol, 1, 10) = '$yourdate'";
execute this and you should be good
Based on the format that you're storing the date as a string, use the STR_TO_DATE function to parse out the date. Then you can use it as a part of the where clause to query desired data.
try this
select * from table where date(your_field)=CURDATE()
or specific date
select * from table where date(your_field)=DATE_FORMAT(2011-05-31 00:02:00, '%Y-%m-%d')

Categories