We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

QueryBuilder with postgreSQL and timestamp.

Hi All,

We're using postgreSQL with our project with column type of "timestamp without time zone". When executing query it gives us string with UTC. Is there any way we can convert this value to unix timestamp while executing query?



39.2k
Accepted
answer

You can implement the afterFetch() method in a model, this event will be executed just after create the instance and assign the data to it. Something like this:

    public function beforeSave()
    {
        $this->timestamp = date("Y-m-d H:s:i", $this->timestamp);
    }

    public function afterFetch()
    {
        $this->timestamp = strtotime($this->timestamp);
    }

    public function afterSave()
    {
        $this->timestamp = strtotime($this->timestamp);
    }


2.1k

Thank You! Works like a charm.



2.1k

Now just wondering if there is any opisite method to query postreSQL timestamp using unixtime. When trying to query I'm getting:

[Thu, 01 Dec 16 12:26:02 +0100][WARNING] SQLSTATE[22008]: Datetime field overflow: 7 ERROR: date/time field value out of range: "1459832523" HINT: Perhaps you need a different "datestyle" setting. in /srv/panel-api.yield.net.pl/app/controllers/Api/BoxesController.php : 66