Home Database Mysql Tutorial sqlite时间函数及时间处理

sqlite时间函数及时间处理

Jun 07, 2016 pm 02:53 PM
sqlite function at deal with time

sqlite时间函数及时间处理 SQLite分页显示:Select * From news order by id desc Limit 10 Offset 这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么翻译不当的地方希望大家指出,毕竟我的英文水平实在很差。 SQLite 包括以下五个时间函数: www.2


sqlite时间函数及时间处理

 

SQLite分页显示:Select * From news order by id desc Limit 10 Offset 

 

这篇文章是根据 SQLite 官方 WIKI 里的内容翻译,如果有什么翻译不当的地方希望大家指出,毕竟我的英文水平实在很差。 SQLite 

包括以下五个时间函数:

   www.2cto.com  

date(日期时间字符串, 修正符, 修正符, ……)

time(日期时间字符串, 修正符, 修正符, 

……)

datetime(日期时间字符串, 修正符, 修正符, ……)

julianday(日期时间字符串, 修正符, 修正符, 

……)

strftime(日期时间格式, 日期时间字符串, 修正符, 修正符, 

……)

上述五个函数需要一个日期时间字符串做参数,后面可以跟零到多个修正符参数。而 strftime() 

函数还需要一个日期时间格式字符串做第一个参数。

  www.2cto.com  

date() 函数返回一个以 “YYYY-MM-DD” 为格式的日期;

time() 函数返回一个以 “YYYY-MM-DD HH:MM:SS” 

为格式的日期时间;

julianday() 函数返回一个天数,从格林威治时间公元前4714年11月24号开始算起;

strftime() 

函数返回一个经过格式话的日期时间,它可以用下面的符号对日期和时间进行格式化:

%d 一月中的第几天 01-31

%f 

小数形式的秒,SS.SSSS

%H 小时 00-24

%j 一年中的第几天 01-366

%J Julian Day 

Numbers

%m 月份 01-12

%M 分钟 00-59

%s 从 1970-01-01日开始计算的秒数

%S 秒 

00-59

%w 星期,0-6,0是星期天

%W 一年中的第几周 00-53

%Y 年份 0000-9999

%% % 

百分号

其他四个函数都可以用 strftime() 函数来表示:

  www.2cto.com  

date(…) -> 

strftime(“%Y-%m-%d”,…)

time(…) 

-> strftime(“%H:%M:%S”,…)

datetime(…) 

-> strftime(“%Y-%m-%d %H:%M:%S”,…)

julianday(…) 

-> strftime(“%J”,…)

日期时间字符串

 

可以用以下几种格式:

 

YYYY-MM-DD

YYYY-MM-DD HH:MM

YYYY-MM-DD HH:MM:SS

YYYY-MM-DD 

HH:MM:SS.SSS

YYYY-MM-DDTHH:MM

YYYY-MM-DDTHH:MM:SS

YYYY-MM-DDTHH:MM:SS.SSS

HH:MM

HH:MM:SS

HH:MM:SS.SSS

now

DDDD.DDDD

在第五种到第七种格式中的“T”是一个分割日期和时间的字符;第八种到第十种格式只代表2000-01-01日的时间,第十一种格式的’now’表示返回一个当前的日期和时间,使用格林威治时间(UTC);第十二种格式表示一个 

Julian Day Numbers。

 

修正符

 

日期和时间可以使用下面的修正符来更改日期或时间:

  www.2cto.com  

NNN days

NNN hours

NNN minutes

NNN.NNNN seconds

NNN months

NNN 

years

start of month

start of year

start of week

start of 

day

weekday 

N

unixepoch

localtime

utc

前六个修正符就是简单的增加指定数值的时间和日期;第七到第十个修正符表示返回当前日期的开始;第十一个修正符表示返回下一个星期是N的日期和时间;第十二个修正符表示返回从1970-01-01开始算起的秒数;第十三个修正符表示返回本地时间。

 

下面举一些例子:

   www.2cto.com  

计算机当前时间

SELECT date(‘now’)

计算机当前月份的最后一天

SELECT date(‘now’,’start of 

month’,’+1 month’,’-1 day’)

计算UNIX 时间戳1092941466表示的日期和时间

SELECT 

datetime(‘1092941466’,’unixepoch’)

计算 UNIX 时间戳1092941466 表示的本地日期和时间

SELECT 

datetime(‘1092941466’,’unixepoch’,’localtime’)

计算机当前UNIX 时间戳

SELECT 

strftime(‘%s’,’now’)

两个日期之间相差多少天

SELECT 

jolianday(‘now’)-jolianday(‘1981-12-23’)

两个日期时间之间相差多少秒

SELECT 

julianday('now')*86400 - julianday('2004-01-01 

02:34:56')*86400

计算今年十月份第一个星期二的日期

SELECT date('now','start of year','+9 

months','weekday 

2');

得到年

strftime(‘%y’,'2008-4-28')

得到月

strftime(‘%m’,'2008-4-28')

同样,我们也可以通过strftime来得到其它所要的信息,但是要记得,给时间加引号

 

例1.

select datetime('now');

结果:2006-10-17 12:55:54

 

例2.

select datetime('2006-10-17');

结果:2006-10-17 12:00:00

   www.2cto.com  

例3.

select datetime('2006-10-17 00:20:00','+1 hour','-12 

minute');

结果:2006-10-17 01:08:00

 

例4.

select date('2006-10-17','+1 day','+1 year');

结果:2007-10-18

 

例5.

select datetime('now','start of year');

结果:2006-01-01 00:00:00

 

例6.

select datetime('now','start of month');

结果:2006-10-01 00:00:00

   www.2cto.com  

例7.

select datetime('now','start of day');

结果:2006-10-17 00:00:00

 

例8.

select datetime('now','+10 hour','start of day','+10 

hour');

结果:2006-10-17 10:00:00

 

例9.

select datetime('now','localtime');

结果:2006-10-17 21:21:47

 

例10.

select datetime('now','+8 hour');

结果:2006-10-17 21:24:45

 

例3中的+1 hour和-12 minute表示可以在基本时间上(datetime函数的第一个参数)增加或减少一定时间。

 

例5中的start of year表示一年开始的时间。

  www.2cto.com  

从例8可以看出,尽管第2个参数加上了10个小时,但是却被第3个参数“start of 

day”把时间归零到00:00:00,随后的第4个参数在00:00:00

的基础上把时间增加了10个小时变成了10:00:00。

 

例9把格林威治时区转换成本地时区。

 

例10把格林威治时区转换成东八区。

 

strftime()函数可以把YYYY-MM-DD 

HH:MM:SS格式的日期字符串转换成其它形式的字符串。

strftime()的语法是strftime(格式, 日期/时间, 修正符, 修正符, 

...)

  www.2cto.com  

它可以用以下的符号对日期和时间进行格式化:

%d 月份, 01-31

%f 小数形式的秒,SS.SSS

%H 小时, 

00-23

%j 算出某一天是该年的第几天,001-366

%m 月份,00-12

%M 分钟, 00-59

%s 

从1970年1月1日到现在的秒数

%S 秒, 00-59

%w 星期, 0-6 (0是星期天)

%W 算出某一天属于该年的第几周, 

01-53

%Y 年, YYYY

%% 百分号

  www.2cto.com  

strftime()的用法举例如下:

例11.

select strftime('%Y.%m.%d 

%H:%M:%S','now','localtime');

结果:2006.10.17 21:41:09

 

例11用圆点作为日期的分隔附,并把时间转换为当地的时区的时间 

 

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1255
29
C# Tutorial
1229
24
Tips for dynamically creating new functions in golang functions Tips for dynamically creating new functions in golang functions Apr 25, 2024 pm 02:39 PM

Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

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

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Comparison of the advantages and disadvantages of C++ function default parameters and variable parameters Apr 21, 2024 am 10:21 AM

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

What are the benefits of C++ functions returning reference types? What are the benefits of C++ functions returning reference types? Apr 20, 2024 pm 09:12 PM

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

How to write efficient and maintainable functions in Java? How to write efficient and maintainable functions in Java? Apr 24, 2024 am 11:33 AM

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

What is the difference between custom PHP functions and predefined functions? What is the difference between custom PHP functions and predefined functions? Apr 22, 2024 pm 02:21 PM

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

C++ Function Exception Advanced: Customized Error Handling C++ Function Exception Advanced: Customized Error Handling May 01, 2024 pm 06:39 PM

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.

See all articles