Parsing the difference between the Unix_timestamp () function in MySQL and the time () function in PHP _php tips

Source: Internet
Author: User
Tags local time
MySQL: Unix_timestamp (), Unix_timestamp (date)
If no parameters are invoked, a UNIX timestamp (the number of seconds after the ' 1970-01-01 00:00:00 ' GMT) is returned as an unsigned integer. If you call Unix_timestamp () with date, it returns the value of the parameter as the number of seconds after the ' 1970-01-01 00:00:00 ' GMT. Date can be a date string, a DateTime string, a timestamp, or a number in the YYMMDD or YYYMMDD format of a local time.
Mysql> SELECT Unix_timestamp ();
-> 882226357
mysql> SELECT unix_timestamp (' 1997-10-04 22:23:00 ');
-> 875996580
When Unix_timestamp is used in the timestamp column, the function returns the internal timestamp value directly without any implied "string-to-unix-timestamp" conversion. If you pass an overflow date to Unix_timestamp (), it will return 0, but be aware that only the basic scope checks will be performed (the year from 1970 to 2037, the month from 01 to 12, and the date from 01 to 31).

Here we can use
From_unixtime (Unix_timestamp), From_unixtime (Unix_timestamp,format) to format a unix_timestamp () timestamp, which will return ' YYYY-MM-DD HH: The Unix_timestamp parameter of the Mm:ss ' or YYYYMMDDHHMMSS format value is expressed in the form of whether the function is used in a string or in a digital context.
If format is already given, the format of the result depends on the format string. The format can contain the same descriptor as the Date_format () function entry list.
Mysql> SELECT from_unixtime (875996580);
-> ' 1997-10-04 22:23:00 '
Mysql> SELECT From_unixtime (875996580) + 0;
-> 19971004222300
Mysql> SELECT From_unixtime (Unix_timestamp (),
-> '%Y%d%m%h:%i:%s%x ');
-> ' 2003 6th August 06:22:58 2003 '

in PHP: Time ()
Time--Returns the current Unix timestamp
Returns the number of seconds since the Unix era (GMT January 1, 1970 00:00:00) to the current time.
Literally they are the same, all returning the number of seconds since the Unix era to the current time.

The author has done a test on the same server and found that the results of the two returns are the same.
Use From_unixtime in MySQL (1156219870, '%y-%m-%d ')
and PHP with date ("y-m-d", 1156219870) The result is the same! The only thing that wouldn't be sure was that the reaction was faster. But I still tend to use the time () function in PHP!
Related Article

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.