Saturday, October 8, 2011

Comparing timestamp in MySQL

Required: MySQL

Using:
table1
         id (int)
         jointime(timestamp)

SQL query
  1. SELECT id FROM table1 WHERE jointime > '2011-10-08 00:00:00';
Explantion:
  1. This query selects id from table where jointime is greater than 2011-10-08.
Note:
  • 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.

No comments:

Post a Comment

Thank you for your comment!