MySQL Forums
Forum List  »  PHP

Re: Date - unix_timestamp
Posted by: Rick James
Date: February 06, 2010 12:27AM

First, observe
mysql> SELECT UNIX_TIMESTAMP(), FROM_UNIXTIME(UNIX_TIMESTAMP());
+------------------+---------------------------------+
| UNIX_TIMESTAMP() | FROM_UNIXTIME(UNIX_TIMESTAMP()) |
+------------------+---------------------------------+
|       1265437741 | 2010-02-05 22:29:01             |
+------------------+---------------------------------+
Then note that you can treat the result of FROM_UNIXTIME as a string:
mysql> SELECT LEFT(FROM_UNIXTIME(UNIX_TIMESTAMP()), 7);
+------------------------------------------+
| LEFT(FROM_UNIXTIME(UNIX_TIMESTAMP()), 7) |
+------------------------------------------+
| 2010-02                                  |
+------------------------------------------+
mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP()) >= '2010-02-01';
+-------------------------------------------------+
| FROM_UNIXTIME(UNIX_TIMESTAMP()) >= '2010-02-01' |
+-------------------------------------------------+
|                                               1 |
+-------------------------------------------------+
(note: "true" and "1" are synonymous.

Recommend:
    WHERE FROM_UNIX_TIME(my_ts) >=          '2010-02-01'
      AND FROM_UNIX_TIME(my_ts) <  DATE_ADD('2010-02-01', 1 MONTH)
Note how this avoids the midnight at the ending boundary, and avoids having to worry about calculating the end of the current month.

Options: ReplyQuote


Subject
Written By
Posted
February 04, 2010 02:38PM
Re: Date - unix_timestamp
February 06, 2010 12:27AM
February 07, 2010 11:33AM
February 07, 2010 12:13PM
February 07, 2010 09:21PM
February 07, 2010 09:43PM
February 07, 2010 10:16PM
February 07, 2010 10:22PM
February 07, 2010 10:31PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.