2

I am trying to use FLOT chart to plot values that i have stored in PostgreSQL. One of the axes will have timestamps. The format that i have stored it the database is: 2013-11-01 00:05:57 (year-month-day hour:minute:second).

I tried the following code to convert this format to UNIX:

$postgresql_timestamp= 2013-11-01 00:05:57;
$unix_timestamp= strtotime($postgresql_timestamp);

the result that o get is 1383260757 wich corresponds to another date: 2013-10-01 23:05:5

I think that the problem that I am having is related with the timestamp format on PostgreSQL.

1
  • It's a timezone problem, your dates are different by ONE hour Commented Nov 21, 2013 at 15:26

2 Answers 2

6

PostgreSQL supports standard SQL's extract() function, which can convert SQL timestamps to Unix timestamps.

select extract(epoch from timestamp '2013-11-01 00:05:57');
Sign up to request clarification or add additional context in comments.

3 Comments

Can you supply a link to such a function? I don't recall seeing that in the docs (and a quick look there didn't turn it up).
@bma: I somehow missed your comment.Sorry about that. But extract() is one of PostgreSQL's Date/Time functions. It's been supported (and documented) since at least PostgreSQL 7.1.
If I was referring to extract(), I have no idea why I'd ask that. I've been using that function since the mid 2000's. A mystery lost in the depths of time...
1

Since I have access to DB with PHP, I used the following code and get good results:

date_default_timezone_set('UTC');

$unix_timstamp = strtotime($postgreSQL_timestamp);

1 Comment

Use extract in your pg_fetch_all(pg_query($handle, "SELECT extract(...) AS epoch, value FROM table;")); gives well calculated epoch timestamps as virtual column 'epoch'. Iterate threw big arrays using PHP to parse timestamps can be slow and inefficient.

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.