Home Database Mysql Tutorial mysql 日期和时间以及转换时间戳函数

mysql 日期和时间以及转换时间戳函数

Jun 07, 2016 pm 05:47 PM
mysql function date time Convert

使用execl转换时间戳的公式为:

代码:
=(xxxxxxxxxx+8*3600)/86400+70*365+19

使用语句解释时间戳语法举例:

代码:

select from_unixtime(1234567890, '%y-%m-%d %h:%i:%s')

附:
在mysql中,一个时间字段的存储类型是int(11),怎么转化成字符类型,比方存储为13270655222,需要转化为yyyy -mm-dd的形式
使用 from_unixtime函数,具体如下:

代码:
from_unixtime(unix_timestamp,format)
返回表示 unix 时间标记的一个字符串,根据format字符串格式化。format可以包含与date_format()函数列出的条目同样的修饰符。
根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %m 月名字(january……december)
%w 星期名字(sunday……saturday)
%d 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(sun……sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(jan……dec)
%j 一年中的天数(001……366)
%h 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%i 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [ap]m)
%t 时间,24 小时(hh:mm:ss)
%s 秒(00……59)
%s 秒(00……59)
%p am或pm
%w 一个星期中的天数(0=sunday ……6=saturday )
%u 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 一个文字“%”。

相关函数

mysql> select something from tbl_name
where to_days(now()) – to_days(date_col)

dayofweek(date)
返回 date 的星期索引(1 = sunday, 2 = monday, ... 7 = saturday)。索引值符合 odbc 的标准。
mysql> select dayofweek('1998-02-03′);
-> 3

weekday(date)
返回 date 的星期索引(0 = monday, 1 = tuesday, … 6 = sunday):
mysql> select weekday('1998-02-03 22:23:00′);
-> 1
mysql> select weekday('1997-11-05′);
-> 2

dayofmonth(date)
返回 date 是一月中的第几天,范围为 1 到 31:
mysql> select dayofmonth('1998-02-03′);
-> 3

dayofyear(date)
返回 date 是一年中的第几天,范围为 1 到 366:
mysql> select dayofyear('1998-02-03′);
-> 34

month(date)
返回 date 中的月份,范围为 1 到 12:
mysql> select month('1998-02-03′);
-> 2

dayname(date)
返回 date 的星期名:
mysql> select dayname(”1998-02-05′);
-> ‘thursday'

monthname(date)
返回 date 的月份名:
mysql> select monthname(”1998-02-05′);
-> ‘february'

quarter(date)
返回 date 在一年中的季度,范围为 1 到 4:
mysql> select quarter('98-04-01′);
-> 2

week(date)
week(date,first)

 

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

Complete collection of excel function formulas Complete collection of excel function formulas May 07, 2024 pm 12:04 PM

Complete collection of excel function formulas

See all articles