0

I'm using the following SQL query in postgres:

SELECT
    date_trunc('month', s.thedate),
    r.rank,
    COUNT(r.rank)
FROM
    serps s
    LEFT JOIN ranks r ON r.serpid = s.serpid
GROUP BY
   date_trunc('month', s.thedate), s.thedate, r.rank
ORDER BY
   s.thedate ASC;

when I run that query directly against the database, I get the data all the data I need and the dates seem to be correct (formatted in Y-m-d g:i:s).

However, when I run it with PHP, Postgres instead of the date returns the timestamp.

Therefore, when I use that timestamp in PHP date, the whole date is incorrect.

For instance:

The first row Postgres displays it as:

"2013-08-01 00:00:00, 36, 1"

but PHP receives:

"1375315200000, 36, 1"

When I try to do:

echo date("Y-m-d", 1375315200000);

The output is:

45552-01-02

instead of

2013-08-01

At first I thought it was a padding issue, perhaps? I dropped the last three zeros in the timestamp so:

echo date("Y-m-d", 1375315200);

and that returns:

2013-07-31

My questions are:

1) Is it only a coincidence that after dropping three zeros, the timestamp represent a day before the actual date stored in the database?

2) Why Postgres interprets the timestamp correctly; whereas php doesn't? According to the documentation Postgres timestamp should be in the unix timestamp format.

5
  • I suspect something with timezone conversion is getting you here. Do you have TimeZone set to UTC on the server? Does your server timezone change? Are the fields in question timestamp or timestamp with time zone type? Dec 11, 2013 at 21:32
  • I think that may be the issue. The database is in Amazon which I'd suspect that the timezone is the one in Seattle, whereas the web server is in Atlanta. But why do I have to drop 3 zeros from the timestamp to get anything close? Is it because postgres timestamp is in miliseconds? I thought it was the same as the unix timestamp.
    – ILikeTacos
    Dec 11, 2013 at 21:34
  • When postgres sends 2013-08-01 00:00:00, that is what php receives. If you think that php receives 1375315200000, please demonstrate it with some code, ideally a reproducible test case. Dec 11, 2013 at 23:10
  • @DanielVérité No, that's not what php receives. The DMA I'm using automatically converts timestamps into dates. I don't have to "demonstrate" that with code, or test cases. Postgres documentation says very clearly: date_trunc returns timestamp.
    – ILikeTacos
    Dec 12, 2013 at 4:00
  • timestamp in this context is a datatype. The values are not a number of milliseconds since 1970. See postgresql.org/docs/current/static/datatype-datetime.html . Plus the output of date_trunc may be of time timestamp or timestamptz depending on the type of thedate field. Dec 12, 2013 at 13:42

1 Answer 1

1

The number they're returning is milliseconds in the Unix era, rather than seconds. Dividing by 1000 before feeding it to PHP is necessary.

With databases, be careful to check whether their timestamp is actually UTC/GMT, or has been offset to the server's timezone. I've seen both done. My server, located in California, is Pacific Time for MySQL timestamps. Be careful about sticking PHP timestamps into the database and then formatting with SQL, or vice-versa.

2
  • that seems to be the case, if that's the case I suspect that I should adjust my php code to date_default_timezone_set($databaseTimezone), right? so that I can display the date correctly.
    – ILikeTacos
    Dec 11, 2013 at 21:42
  • PHP will think that the integer timestamp it receives (approximately 1.3 billion right now) is GMT/UTC. The cleanest thing would probably be to add or subtract some number of seconds to the DB timestamp to get GMT/UTC. That could get messy, though, if the DB server is obeying DST rather than just year-round standard time.
    – Phil Perry
    Dec 11, 2013 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.