search
Homephp教程php手册PHP和Mysql的Timestamp互换

PHP和Mysql的Timestamp互换

Jun 12, 2016 pm 08:06 PM
mysqlphptimestampcodeOpen sourceprogrammingprogramming languagesoftware development

   在mysql中有三种时间字段类型:DATETIME,DATE和TIMESTAMP。

    DATETIME以YYYY-MM-DD HH:MM:SS格式的字符串来保存数据;

    DATE则是只有年月日以YYYY-MM-DD形式的字串;

  TIMESTAMP类型和PHP中的TIMESTAMP类型名字一样,但是两者基本上是不同的。

  PHP是延用了UNIX时间签的类型为一个整数,

  而在mysql中TIMESTAMP字段则是随着记录变化而一个自动更新为当时时间的DATETIMP字段。在mysql4.1版本之后TIMESTAMP格式DATETIME格式基本上是一致了。

  于是常常需要在php和msql中对两种格式的Timestamp进行转换。转换方法总结一下:

    第一种方法:使用 date()和strtotime()函数

    $mysqltime=date('Y-m-d H:i:s',$phptime);

    $phptime=strtotime($mysqldate);

    第二种方法:在查询语句中使用mysql函数转换:UNIX_TIMESTAMP(DATETIME=>PHP TIMESTAMP)和FROM_UNIXTIME(PHP TIMESTAMP=>DATETIME).

    $sql="SELECT UNIX_TIMESTAMP(datetimefield) FROM table WHERE ...";

    $sql="UPDATE table set datetimefield=FROM_UNIXTIME($phptime) WHERE ..";

    第三种方法:就是mysql中使用整数字段来保存php的timestamp类型。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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