Handling Date(Time)S In Activerecord

I’m creating my first project with Yii2, and stumbled upon a design decision about dates.

Many of my database models have date and/or datetime attributes.

I’m trying to stay as close as possible to Yii2’s default design choices, so I have converted my DB columns to unsigned ints, which store UNIX timestamps.

However, gii interpreted my timestamps as integers (creating date columns in MySQL doesn’t help: this results in string variables).

I could write getter/setter methods for each date or time attribute in each model, to handle the conversion between timestamps and readable dates. However, I’m curious whether there isn’t a more refined method.

Can I circumvent writing all those getter/setter methods somehow? For example by indicating that it are dates.

I have noticed that the validator uses PHP’s new Date/Time classes. Would it make sense to have my model return these attributes as such objects? Will this somehow help me with using the dates/times in queries or to parse them in AR forms using the JUI date picker?

I’m always impressed with the brevity and elegance of the framework’s code. Writing a lot of similar getters/setters in my code seems to violate the elegance of the framework…


I just noticed that the standard ‘view.php’ generated by Gii does interpret my timestamps as dates (it is showing full dates instead of timestamp integers).

Where is this behavior determinded? How can I combine it with JUI date pickers in my form?

thanks in advance!

I think a code example of how to handle dates, all the way from database table (column data type) to JUI in form, and back (to save the dates into updated timestamps) would be incredibly useful.

This is functionality that most apps will need. By setting a best practice now, not everyone building an app will need to reinvent the wheel… Just my 2 cents!

Not a best practice, probably, but I do it this way:

Step 1. Define getter and setter in your model.

public function getCreated()


    return $this->create_time ? date('d-m-Y', $this->create_time) : null;


Step 2. Use virtual attribute (‘created’ in the example above) for datepicker.

Dont’ forget about validation rules for your new attribute.

@Orey: that’s also what I was thinking.

However, since the standard view generated by Gii is able to convert my Unix timestamp to a readable format automatically, I was hoping for the framework to offer some kind of built-in solution.

After all, the whole reason why I use a framework is to avoid having to think about "trivial" issues like how to store and convert dates…

  1. I don’t use Gii at all, sorry. Cmd+C / Cmd+V do all the stuff.

  2. As for me, the framework must be as clean as possible, without extra magic. That’s why I don’t use RoR: it’s too magic :)

  3. And yes, I hate CGridViews and all the magic widgets like bootstrap extensions. They do things that can be done by a couple of lines of code, but bring in a lot of trouble for those who use them (this forum is full of posts like "I cannot do this in that widget, plz help").

Storing and retrieving timestamps can be done exactly in two lines of code (one for getter and one for setter), so no, I don’t think it’s a good idea to bring this in the core.

Alright, thanks for the feedback. I’ll go with getters and setters!