search
Homephp教程php手册PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)

PHP+Mysql日期时间如何转换(UNIX时间戳和格式化日期)

Jun 13, 2016 am 11:59 AM
php+mysqlunixandhowdatetimehaveformatofKnowprogrammerConvert

写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式。

一、在MySQL中完成

  这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性。

1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME()

一般形式:select FROM_UNIXTIME(1156219870);

2. 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()

一般形式:Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);

举例:mysql查询当天的记录数:

$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”;

当然大家也可以选择在PHP中进行转换,下面说说在PHP中转换。

二、在PHP中完成

  这种方式在PHP程序中完成转换,优点是无论是不是数据库中查询获得的数据都能转换,转换范围不受限制,缺点是占用PHP解析器的解析时间,速度相对慢。

1. UNIX时间戳转换为日期用函数: date()

一般形式:date('Y-m-d H:i:s', 1156219870);

2. 日期转换为UNIX时间戳用函数:strtotime()

一般形式:strtotime('2010-03-24 08:15:42');

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

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.