Re: php, time and postgresql - Mailing list pgsql-general

From Tom Lane
Subject Re: php, time and postgresql
Date
Msg-id 21717.979337291@sss.pgh.pa.us
Whole thread Raw
In response to php, time and postgresql  (Rasmus Resen Amossen <spunk@rhk.dk>)
List pgsql-general
Rasmus Resen Amossen <spunk@rhk.dk> writes:
> I want to save some time information in postgresql with php. What is the
> datatype in postgresql coresponding to the mktime()-unixtime function in
> php (creates a time in the format "979333398" eg.)?

You can coerce an integer representing a Unix timestamp to abstime,
and thence to any other datetime datatype you might want to use.

play=> select now()::abstime::integer;
 ?column?
-----------
 979337141
(1 row)

play=> select 979337141::integer::abstime;
        ?column?
------------------------
 2001-01-12 17:05:41-05
(1 row)

The "official" way to get from a datetime type to a Unix timestamp is
date_part('epoch', timevalue):

play=> select date_part('epoch', now());
 date_part
-----------
 979337212
(1 row)

but I don't know of any easy way to go in the other direction except by
casting to abstime.

            regards, tom lane

pgsql-general by date:

Previous
From: Alfred Perlstein
Date:
Subject: Re: FreeBSDers paying attention? (was Re: exit status 26)
Next
From: Tom Lane
Date:
Subject: Re: [HACKERS] problems with pg_geqo