MySQL documentation is in general extremely vague about the data types returned by functions and UNIX_TIMESTAMP()
is not an exception. Unless we check the source code I think we can only make an educated guess.
At Date and Time Type Overview we can read that the TIMESTAMP
data type itself has a documented range that doesn't depend on the server architecture:
The range is '1970-01-01 00:00:01.000000' UTC to
'2038-01-19 03:14:07.999999' UTC. TIMESTAMP values are stored as the
number of seconds since the epoch ('1970-01-01 00:00:00' UTC). A
TIMESTAMP cannot represent the value '1970-01-01 00:00:00' because
that is equivalent to 0 seconds from the epoch and the value 0 is
reserved for representing '0000-00-00 00:00:00', the “zero” TIMESTAMP
value.
Even if we make sure we pass a proper date type:
mysql> select
-> str_to_date('2038-01-20', '%Y-%m-%d'),
-> unix_timestamp(str_to_date('2038-01-20', '%Y-%m-%d'));
+---------------------------------------+-------------------------------------------------------+
| str_to_date('2038-01-20', '%Y-%m-%d') | unix_timestamp(str_to_date('2038-01-20', '%Y-%m-%d')) |
+---------------------------------------+-------------------------------------------------------+
| 2038-01-20 | 0 |
+---------------------------------------+-------------------------------------------------------+
1 row in set (0.01 sec)
... we still get 0
, the silly function flag for errors:
If you pass an out-of-range date to UNIX_TIMESTAMP(), it returns 0.
So it's kind of safe to assume that UNIX_TIMESTAMP()
returns a value of TIMESTAMP
type thus 2038+ is not supported.
In short: you'll have to calculate timestamps somewhere else (i.e., your client code). Since there's a PHP tag:
$t = new DateTime('2038-01-20', new DateTimeZone('UTC'));
var_dump( $t->format('U') );
string(10) "2147558400"
P.S. MariaDB, the MySQL fork, has the same restriction but it documents it better:
Timestamps in MariaDB have a maximum value of 2147483647, equivalent
to 2038-01-19 05:14:07. This is due to the underlying 32-bit
limitation. Using the function on a date beyond this will result in
NULL being returned.