Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to parse psql timestamp

I display the date or the time on my website a lot and I'm thinking about writing a function to parse a PostgreSQL timestamp.

The timestamp is in the format: Y-m-d H:i:s.u. E.g. 2011-04-08 23:00:56.544.

I'm thinking about something like this:

function parse_timestamp($timestamp, $format = 'd-m-Y')
{
    // parse the timestamp

    return $formatted_timestamp;
}

However I am wondering whether this can also be achieved without writing a parser for it myself (with the use of some PHP function).

like image 407
PeeHaa Avatar asked Dec 10 '22 09:12

PeeHaa


1 Answers

function parse_timestamp($timestamp, $format = 'd-m-Y')
{
    return date($format, strtotime($timestamp));
}

Don't forget to set timezone before, e.g.

date_default_timezone_set('UTC');

Or in your case, I guess 'Europe/Amsterdam'.

You can always get PHP timestamp of this format Y-m-d H:i:s.u using strtotime(). Then, using date() you can export time in your own format. Both functions depend of time zone set.

like image 190
Wh1T3h4Ck5 Avatar answered Dec 22 '22 23:12

Wh1T3h4Ck5