Comparing timestamp in MySQL

We often use timestamps in databases. In fact people often use it to make session keys. Comparing timestamp is also often required when detecting any change in database. That’s too much often. 😛

Required: MySQL


Table defination

id (int)

SQL query

SELECT id FROM table1 WHERE jointime > '2011-10-08 00:00:00';


  1. This query selects id from table where jointime is greater than 2011-10-08.


  • Timestamp is a standard data type in MySQL and it should have both date and time.
  • For current time use NOW()
  • If you are using only date then use conversion from date to timestamp using TIMESTAMP()
  • With PHP
    • First get the time if current time use time(), it will give time in Unix Epoch.
    • Convert above time to timestamp using date() and then use in query.
    • If using some other time use mktime().
  • Remember the quotes they are important.

Also see MySQL timestamp datatype and PHP date time functions, also other functions.