Resolve the differences between UNIX_TIMESTAMP of in mysql and time of in php

  • 2020-06-19 09:54:14
  • OfStack

mysql: UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)
If invoked without arguments, 1 Unix timestamp (the number of seconds after '1970-01-01 00:00:00' GMT) is returned as an unsigned integer. If you call UNIX_TIMESTAMP() with date, it returns the parameter value as '1970-01-01 00:00:00' GMT in seconds. date can be 1 DATE string, 1 DATETIME string, 1 TIMESTAMP, or 1 YYMMDD or YYYMMDD local time number.
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 implicit "ES40en-ES41en-ES42en-ES43en" conversion. If you pass UNIX_TIMESTAMP() an overflow date, it returns 0, but note that only basic range checks are performed (years 1970 to 2037, months 01 to 12, and dates 01 to 31).

We can use it here
FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format) formats an UNIX_TIMESTAMP() timestamp, which returns the unix_timestamp parameter representation of the 'YYYY-ES61en-ES62en HH:MM:SS' or YYYYMMDDHHMMSS format value, depending on whether the function is used in a string or in a numeric context.
If format has been given, the format of the result is based on the format string. format can contain the same specifier as in the list of DATE_FORMAT() function input entries.
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 the php time ()
time - returns the current Unix timestamp
Returns the number of seconds from the Unix era (00:00:00 GMT, 1 January 1970) to the current time.
They are literally one and the same, both return the number of seconds from THE Unix era to the current time.

The author conducted a test on the same server and found that the results returned by the two servers were the same.
FROM_UNIXTIME(1156219870,'% es120EN-% d')
The result is the same as date(" ES126en-ES127en-ES128en ",1156219870). The only thing that one doesn't know for sure is which one responds more quickly. But I prefer to use the time () function in php!

Related articles: