1

I don't know why, but I like to work with Unix-timestamp. Maybe because I need to perform calculations with time like with number, e.g. time()-604800.

Right now I have integer column whereto I insert result of time(). I think it isn't right way, is it? Is there a default value -- unix timestamp?

1 Answer 1

10

You should get rid of the habit of using unix timestamps in your database. PostgreSQL has really awesome date functions such as datetimecol - interval '1 week'. It can also easily convert to unix timestamps using extract(epoch from datetimecol).

Anyway, you can use extract(epoch from now()) as the default value of your column.

Sign up to request clarification or add additional context in comments.

3 Comments

I think it's from MySQL, before I've been using it and I didn't like time functions in it. And recently I've decided that MySQL doesn't fit my needs and I've chose PgSql. :)
Guessed so. Take it as a reason to get away from bad habits. In case you are using PHP also start using PDO or at least pg_query_params() instead of escaping.
Be sure to use TIMESTAMP WITH TIME ZONE (also known as timestamptz); the default TIMESTAMP (which is "WITHOUT TIME ZONE") does not represent a moment in time.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.