Thursday, April 28, 2011

A PostgreSQL timestamp field not formated in the same way when updated with PHP.

I have a timestamp with timezone field in PostgreSQL. When I update this field, I use something like this:

$date = date('Y-m-d H:i:s');

Although the SQL works fine, the date saved seem a little bit different to a classic timestamp with timezone date.

Example:

Default value set to "now()":
date 2009-04-06 14:39:53.662522+02

Update with a date set in php:
$date = date('Y-m-d H:i:s');
date 2009-04-06 14:39:53+02

The numbers removed on update are probably milliseconds but I'm not sure. I would like to know if there is a means to obtain with PHP the same format of date?

From stackoverflow
  • You can use the microtime() PHP function to get the microseconds.

  • If you only need one second resolution of timestamp you have to design your database accordingly, as by default resolution is better than a second.

    Use for example the following column definition:

    last_access_time timestamp with time zone not null
        default date_trunc('second',now())
        constraint last_access_time_full_second check (
            date_trunc('second',last_access_time)=last_access_time
        )
    
    Milen A. Radev : Fortunatelly the column definition could be much simpler (at least the part for the default value): last_access_time timestamp(0) with time zone default CURRENT_TIMESTAMP

0 comments:

Post a Comment