getting data for specific months from date in mysql - php

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';

Related

Past and present event from mysql database compare today by using php

i want to get past and future events from database and by using php-mysql query, and i had try with this query
select * from table name where date >date();
but i got the past events in future section. and i saved date in varchar data type.
please help me
Thank You
Use NOW() MySQL function to get current timestamp and STR_TO_DATE function to convert string date to date format
select * from table_name where STR_TO_DATE(date, '%Y-%m-%d %H:%i:%s') > now();
But you really should consider to alter your table to TIMESTAMP data type

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

Inserting date to database in mysql

I use NOW() function but I get this weird date:
2011-11-06
How do I get the following European date:
ss:mm:hh dd:mm:year (06-11-2011)
In my database I set the field column date as DATE
How do you integrate DATEFORMAT into this query:
$avatar_Q=mysql_query("
SELECT user_name,avatar,reputation,comment, DATE_FORMAT(date,'%d/%m/%Y %h:%i') AS rightNow
FROM comments AS com
INNER JOIN users AS us ON com.user_id=us.user_id
WHERE comment_id=$commentID
") or die(mysql_error());
The date is is in the column table
MySQL uses the following date format - YYYY-MM-DD - if you want a different format you need to use the DATE_FORMAT function on select
for example :
SELECT DATE_FORMAT(datecolumn,"%d/%m/%Y %h:%i")
FROM atable
To integrate the date_format function into your select statement you need to list the fields individually
Yep, use a Timestamp column and insert with CURRENT_TIMESTAMP. It saves a lot of time! :)
My personal recommendation is to save the date as a UNIX Timestamp (using the time() function) ,
This way you could format it as you wish .
Shai.
DATE_FORMAT
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
That format is just how MySQL stores the data type DATE: http://dev.mysql.com/doc/refman/5.5/en/datetime.html.
If you're using the DATE data type for you column you can either:
Use the DATE_FORMAT function in your SQL to get the date in your desired format
Use a combination of PHP's strtotime and date functions to display your date in the most appropriate format

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