Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date in MySQL to return MonthName, Year

I have rows in a table that I need to order by date. Easy enough, use the date datatype to store them, ORDER BY date blah blah.

However, I'm trying to get these rows, and easily convert the date to MonthName, Year

If I had stored a row's date as 2011-11-16, I would want to extract this (using PHP) and print out:

 November, 2011
like image 871
Andrew Avatar asked Nov 17 '11 02:11

Andrew


People also ask

How do I format a date in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format.

How can get date in dd mm yyyy format in MySQL?

The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well.

How convert date format from DD MM YYYY to Yyyymmdd in MySQL?

Use STR_TO_DATE() method from MySQL to convert. The syntax is as follows wherein we are using format specifiers. The format specifiers begin with %. SELECT STR_TO_DATE(yourDateColumnName,'%d.

What is %d in MySQL?

%d – the argument is treated as an integer, and presented as a (signed) decimal number. %s – the argument is treated as and presented as a string. in your examples, $slug is a string and $this->id is an integer.


3 Answers

Try this

SELECT DATE_FORMAT(`date`,'%M %Y') AS showdate FROM table
like image 188
Nalaka526 Avatar answered Oct 21 '22 01:10

Nalaka526


You can get date year and monthName formate using the following query.

SELECT DATE_FORMAT("2017-06-15", "%Y %M") as 'Date';

If you need other formats apply following combinations.

Syntax: DATE_FORMAT(date, format_mask)

Format  Description
%a  Abbreviated weekday name (Sun to Sat)
%b  Abbreviated month name (Jan to Dec)
%c  Numeric month name (0 to 12)
%D  Day of the month as a numeric value, followed by suffix (1st, 2nd, 3rd, ...)
%d  Day of the month as a numeric value (01 to 31)
%e  Day of the month as a numeric value (0 to 31)
%f  Microseconds (000000 to 999999)
%H  Hour (00 to 23)
%h  Hour (00 to 12)
%I  Hour (00 to 12)
%i  Minutes (00 to 59)
%j  Day of the year (001 to 366)
%k  Hour (0 to 23)
%l  Hour (1 to 12)
%M  Month name in full (January to December)
%m  Month name as a numeric value (00 to 12)
%p  AM or PM
%r  Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
%S  Seconds (00 to 59)
%s  Seconds (00 to 59)
%T  Time in 24 hour format (hh:mm:ss)
%U  Week where Sunday is the first day of the week (00 to 53)
%u  Week where Monday is the first day of the week (00 to 53)
%V  Week where Sunday is the first day of the week (01 to 53). Used with %X
%v  Week where Monday is the first day of the week (01 to 53). Used with %X
%W  Weekday name in full (Sunday to Saturday)
%w  Day of the week where Sunday=0 and Saturday=6
%X  Year for the week where Sunday is the first day of the week. Used with %V
%x  Year for the week where Monday is the first day of the week. Used with %V
%Y  Year as a numeric, 4-digit value
%y  Year as a numeric, 2-digit value
like image 10
Ganesh Giri Avatar answered Oct 21 '22 00:10

Ganesh Giri


SELECT DATE_FORMAT(date_col, '%M, %Y')
FROM   tbl
-- WHERE ??
ORDER  BY date_col;

The manual knows more about that.

like image 4
Erwin Brandstetter Avatar answered Oct 21 '22 01:10

Erwin Brandstetter