Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



25.3k
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);
    }


472

Thank You! Works like a charm.



472

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