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.

Date Time with RFC3339 format

  • Storing and retreiving datetime from model by RFC 3339 standard
  • any suggestion on how to do it Phalcon way?
protected $createdAt;

public function getCreatedAt()
{
    return date('Y-m-d\TH:i:sP', $this->createdAt);
}

public function setCreatedAt($value)
{
    $this->createdAt = date('YOUR DB FORMAT "U" FOR UNIXTIMESTAMP', strtotime($value));
}

As long as the property is protected the model will use the public accessors to interact with it, so this will work like a charm:

``` echo $robot->createdAt; $robot->createdAt = date('d/m/Y', time() - 86400);



46.5k
Accepted
answer
edited Jul '16
DateTime::RFC3339
DATE_RFC3339
    //Same as DATE_ATOM (since PHP 5.1.3)

Working example:

$d = new DateTime();
echo $d->format(DATE_RFC3339); //2016-07-04T17:53:30+02:00
  • set timezone in index.php (and in cli starting page, ...)
  • in database save always as timestamp(int)
  • output as RFC-3339, shouldn't a better way then in Model, to put it in Transformer??? (http://fractal.thephpleague.com/transformers/)
  • in what format to fetch a datetime from client?
edited Jul '16

You set your timezone and anything time zone related stuff in main bootstrap file, and both your web app and CLI app can inhreit that settings. For instance, I keep such settings in config.ini so they can be easily changed by non-developers.

You don't have to save int (timestamp) in a database as such, at least for MySQL/MariaDB since timestamp column will have that by default in a background for you so you can query it with timestamp or with regular human readable date and time.

RFC-3339 provided here is PHP way, so Phalcon can only use that. You can put it whenever you want, in Models too, why not? The important part is that you can get ouput of the preferred date format in one line.

Fetching datetime from a client is a bad idea IMHO, it's always better to use time() i.e. timestamp on the server side and to present human readable format to the client.

For a database structure, here's what I use:

  `request_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
  `response_timestamp` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP

Pozdrav! :)