Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:26:57+00:00 2026-06-11T16:26:57+00:00

I have a query that unfortunately has to compare 2 timestamps. One timestamp is

  • 0

I have a query that unfortunately has to compare 2 timestamps. One timestamp is given to the DB from the PHP date() function, stored as timestamp without time zone, so there’s no milliseconds added to this date. The other is a PG timestamp with time zone. So both dates have already been created and inserted into the tables, here is an example of the dates:

timestamp without time zone = 2012-09-19 18:13:26

PG’s timestamp with time zone = 2012-09-19 18:13:26.893878-04

I can cast the PG date::timestamp(0) which gets me close but as would be expected the date is rounded up; 2012-09-19 18:13:27

So my question is how can I get the seconds to round down?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:26:58+00:00Added an answer on June 11, 2026 at 4:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like this SELECT * from myTable WHERE Date
I have a query that successfully grabs the unique products from my products table
I have a query that basically combines tables of actions and selects from them
I have a query that is pulling users who liked a specific object from
I have a function runAjax that functions correctly. Unfortunately I am struggling to return
I have a table that has three columns: Category, Timestamp and Value. What I
I have a query string passed in through an HTTP request that has this
I have a ko.computed() function that proxies a collection like this: function ListView(query) {
I'm trying to construct a query that will map two columns, one, a date
I am using in C# MYsql .I have query that works if I run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.