Home >Database >Mysql Tutorial >How to convert mysql date to string

How to convert mysql date to string

藏色散人
藏色散人Original
2020-02-08 10:42:0615196browse

How to convert mysql date to string

How to convert mysql date to a string?

Mysql date to string method

Mysql convert date to string

select date_format(time, '%Y-%m-%d %H:%i:%s') from  info   # 2019-08-22 21:03:21

MySQL DATE_FORMAT() function

MySQL Date function

Definition and usage

DATE_FORMAT() function is used to display in different formats Date/time data.

Syntax

DATE_FORMAT(date,format)

The date parameter is a legal date. format specifies the date/time output format.

The formats that can be used are:

Format and description

%a abbreviated week name

%b abbreviated month name

%c Month, value

%D Day of the month with English prefix

%d Day of the month, value (00-31)

%e Day of the month, value (0-31)

%f microseconds

%H hours (00-23)

%h hours (01-12)

%I hour (01-12)

%i minute, value (00-59)

%j day of year (001-366)

%k hour (0-23)

%l hour (1-12)

%M month name

%m month, value (00-12)

%p AM or PM

%r Time, 12-hour (hh:mm:ss AM or PM)

%S seconds (00-59)

%s Seconds (00-59)

%T Time, 24-hour (hh:mm:ss)

%U Week (00-53) Sunday is the first day of the week Day

%u Week(00-53) Monday is the first day of the week

%V Week(01-53) Sunday is the first day of the week, used with %X

%v Week (01-53) Monday is the first day of the week, and %x uses

%W week name

%w day of the week (0= Sunday, 6=Saturday)

%X years where Sunday is the first day of the week, 4 digits, used with %V

%x years where Monday is the first day of the week Day 1, 4 digits, with %v using

%Y year, 4 digits

%y year, 2 digits

Instance

The following script uses the DATE_FORMAT() function to display different formats. We use NOW() to get the current date/time:

DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')

The result is similar:

Dec 29 2008 11:45 PM
12-29-2008
29 Dec 08
29 Dec 2008 16:25:46.635

(free learning video tutorial recommendation: mysql video tutorial)

The above is the detailed content of How to convert mysql date to string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Usage of mysqli_query()Next article:Usage of mysqli_query()