mysql> desc test200; +---------------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | registerTime | datetime | YES | | NULL | | | lastLoginTime | datetime | YES | | NULL | | +---------------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> select * from test200; +------+---------------------+---------------------+ | id | registerTime | lastLoginTime | +------+---------------------+---------------------+ | 1 | 2016-10-21 09:06:23 | 2016-10-21 09:06:56 | | 1 | 2016-10-21 09:16:11 | 2016-10-21 09:16:11 | +------+---------------------+---------------------+ 2 rows in set (0.00 sec) mysql> select lastLoginTime - registerTime from test200; +------------------------------+ | lastLoginTime - registerTime | +------------------------------+ | 33 | | 0 | +------------------------------+ 2 rows in set (0.00 sec) mysql> select (UNIX_TIMESTAMP(lastLoginTime) - UNIX_TIMESTAMP(registerTime)) from test200; +----------------------------------------------------------------+ | (UNIX_TIMESTAMP(lastLoginTime) - UNIX_TIMESTAMP(registerTime)) | +----------------------------------------------------------------+ | 33 | | 0 | +----------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql> select now(),lastLoginTime,(UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(lastLoginTime)) from test200; +---------------------+---------------------+---------------------------------------------------------+ | now() | lastLoginTime | (UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP(lastLoginTime)) | +---------------------+---------------------+---------------------------------------------------------+ | 2016-10-21 09:32:32 | 2016-10-21 09:06:56 | 1536 | | 2016-10-21 09:32:32 | 2016-10-21 09:16:11 | 981 | +---------------------+---------------------+---------------------------------------------------------+ 2 rows in set (0.01 sec)