PHP and MySQL timestamp interchange

Source: Internet
Author: User
Tags php and mysql

There are three types of time fields in MySQL:

Datetime,date and timestamp.

DateTime holds the data in YYYY-MM-DD HH:MM:SS format string;

Date is a string of yyyy-mm-dd in the form of only a month and a day;

The timestamp type has the same name as the timestamp type in PHP, but the two are basically different.


PHP is the type of Unix time signature that is followed by an integer, while in MySQL the timestamp field is changed with the record and a

Automatically updated to the Datetimp field of the time. After the mysql4.1 version of the timestamp format, the datetime format is basically consistent.

It is often necessary to convert the timestamp in both formats in PHP and mSQL. The conversion method summarizes:

The first method: Use the date () and strtotime () functions

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

$phptime =strtotime ($mysqldate);

The second method: Use the MySQL function transformation in the query statement:

Unix_timestamp (datetime=>php TIMESTAMP) and

From_unixtime (PHP timestamp=>datetime).

$sql = "Select Unix_timestamp (Datetimefield) from table WHERE ...";

$sql = "UPDATE table set Datetimefield=from_unixtime ($phptime) WHERE:";

The third Way: MySQL uses integer fields to save PHP's timestamp type.

This article is from the "Sheep" blog, please make sure to keep this source http://5934497.blog.51cto.com/5924497/1683518

PHP and MySQL timestamp interchange

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.