Mysql, How to custimize timestemp while selecting data - php

Please I have a field date type timestamp (format 2013-03-29 14:32:34).
I want to select data from the table and group them by the field date but without time, I mean only by the format Y-m-d.
So I've tried :
SELECT date (Y-m-d) FROM table GROUP BY date (Y-m-d)
This return false. Please How could I do ? Thanks in advance.

To extract the date part of the datetime, use DATE()
SELECT DATE(date) FROM ...
GROUP BY DATE(date)

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

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.

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

Retrieve data for a time interval from a DATETIME Column MySQL /PHP

Im pulling information from a database and ive got the query working fine except i'd like to only select conent from a certain date range. Each row has a field with the created date stored as a DATETIME field. What is the basic syntax?
SELECT fields
FROM table
WHERE date BETWEEN '$startDate' AND '$endDate'
Dates in MySQL are in YYYY-MM-DD format, so the actual query would look like:
SELECT fields
FROM table
WHERE date BETWEEN '2010-10-01' AND '2010-09-25'
WHERE `date_field` BETWEEN '2010-09-21 12:13:14' AND '2010-09-28 12:13:14'
Here's the link
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_between
select * from table where datetime between DATE1 and DATE2
SELECT * FROM table
WHERE DateTime
BETWEEN time1 AND time2

Categories