Comparing timestamps for equality is rarely going to work well. What if the two timestamps were taken at 2012-09-19 18:13:26.99999999
? Clock jitter, scheduler jitter, execution time differences, etc could and often will push one over into the next second. It doesn't have to be that close to the edge to happen, either. You can try hacks with
Compare with a tight range instead; say 2 seconds:
SET timezone = '+04:00';
SELECT (TIMESTAMP '2012-09-19 18:13:26')::timestamptz
BETWEEN TIMESTAMPTZ '2012-09-19 18:13:26.893878-04' - INTERVAL '1' SECOND
AND TIMESTAMPTZ '2012-09-19 18:13:26.893878-04' + INTERVAL '1' SECOND;
I'm not sure you can use anything coarser than that because the precision of your PHP timestamp is 1 second.
If you know for sure that PHP always truncates the timestamp (rounds down) rather than rounding to even when it captures the timestamp, you can roughly correct for this by adjusting the bracketing intervals. Eg to attempt a 1 second interval (the narrowest you can test for given your timestamp precision from PHP) try, and assuming PHP always truncates the timestamp down:
SELECT (TIMESTAMP '2012-09-19 18:13:26')::timestamptz
BETWEEN TIMESTAMPTZ '2012-09-19 18:13:26.893878-04' - INTERVAL '1' SECOND
AND TIMESTAMPTZ '2012-09-19 18:13:26.893878-04';
Personally I'd add at least another 0.1 second each side to be sure:
SELECT (TIMESTAMP '2012-09-19 18:13:26')::timestamptz
BETWEEN TIMESTAMPTZ '2012-09-19 18:13:26.893878-04' - INTERVAL '1.1' SECOND
AND TIMESTAMPTZ '2012-09-19 18:13:26.893878-04' + INTERVAL '0.1' SECOND;
If you really insist on testing for equality, use:
regress=# SELECT date_trunc('second', TIMESTAMPTZ '2012-09-19 18:13:26.893878-04');
date_trunc
------------------------
2012-09-19 18:13:26-04
(1 row)
but be aware it's dangerous and wrong to test two separately captured timestamps for equality.