1

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?

2 Answers 2

1

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
    )
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

Comments

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.