Differences between the UNIX_TIMESTAMP () function in mysql and the time () function in php
Source: Internet
Author: User
In mysql: UNIX_TIMESTAMP (), UNIX_TIMESTAMP (date) If no parameter is called, a Unixtimestamp (& amp; #39; 1970-01-0100:00:00 & amp; #39; is returned; the number of seconds after GMT. If you use date to call UNIX_TIMESTAMP (), it will: UNIX_TIMESTAMP (), UNIX_TIMESTAMP (date) in mysql)
If no parameter is called, a Unix timestamp ('2017-01-01 00:00:00 'seconds after GMT) is returned as an unsigned integer. If you use date to call UNIX_TIMESTAMP (), it will return the parameter value in the form of the number of seconds after '2017-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 the local time.
Mysql> SELECT UNIX_TIMESTAMP ();
-> 882226357
Mysql> SELECT UNIX_TIMESTAMP ('2017-10-04 22:23:00 ');
-> 875996580
When UNIX_TIMESTAMP is used in the TIMESTAMP column, the function returns an internal timestamp value without any implicit conversion of "string-to-Unix-TIMESTAMP. If you pass an overflow date to UNIX_TIMESTAMP (), it returns 0, but note that only the basic range check will be executed (year from 1970 to 2037, month from 01 to 12, from 01 to 31 ).
Here we can use
FROM_UNIXTIME (unix_timestamp), FROM_UNIXTIME (unix_timestamp, format) to format a UNIX_TIMESTAMP () timestamp, which returns 'yyyy-MM-DD HH: MM: the unix_timestamp parameter of the SS' or YYYYMMDDHHMMSS format value. the specific format depends on whether the function is used in a string or a digital context.
If the format is provided, the format of the result is determined by the format string. Format can contain the same specifiers as in the input item list of the DATE_FORMAT () function.
Mysql> SELECT FROM_UNIXTIME (875996580 );
-> '2017-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 ');
-> '2014 6th August 06:22:58 123'
In php: time ()
Time -- returns the current Unix timestamp
Returns the number of seconds from the Unix epoch (January 1, January 1, 1970 00:00:00 GMT) to the current time.
Literally, they are the same. they all return the number of seconds from the Unix epoch to the current time.
I did a test on the same server and found that the results returned by the two are the same.
Using FROM_UNIXTIME (1156219870, '% y-% m-% d') in mysql ')
The results are the same as those of date ("y-m-d", 1156219870) in php! The only uncertain thing is that the response is faster. However, I prefer to use the time () function in php!
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.