Format The Datetime Value In Yii 2

The datetime value saved in database is "2014-06-30 02:52:51.210201"

This is also the value displayed by Yii,

what is the best approach to display it as "30-06-2014 02:52:51" everywhere ?

found this in Yii’s wiki but don’t know how to use it

Solved by using DateTime

public function getFormattedCreateTime()


    return DateTime::createFromFormat('Y-m-d H:i:s.u', $this->create_time)->format('d-m-Y H:i:s');


But how can I search for formattedCreateTime attribute of this model using GridView ?


Did you find any solution ? I’m laso interested.

Kinds Regards,

Seconded, I am also struggling with this. (noob)

Was trying to post this about 4 hours ago, but being new, it would not let me post more than 3 posts in my first 24 hours!

Anyway, now the mods know I am not a bot.

I was trying out this pluggin, I could not get it working globally, but apparently I am missing something as everyone else using it seems fine:

Anyhow, thought it might help you guys, can you let me know if you get it working as I need some help with it. Good Luck.


To search for magic properties in GridView follow this tutorial.

The tutorial shows how to create search filters for related columns but it works exactly the same for magic properties.

Your andFilterWhere function will need to look something like also …

andFilterWhere(['like', 'SQL_TO_GET_DATE_IN_SAME_FORMAT_AS_MAGIC_PROPERTY', $this->magicProperty])

Have a go, if you still cannot get it to work I can help you if you give me your Skype.

Column in datagrid for magic property "countCompletedTasks" …



	"attribute" => "countCompletedTasks"



Magic property in model …

public function getCountCompletedTasks() {


	return count($this->completedTasks);



Code in search model …



	namespace app\models;


	use Yii;

	use app\models\Milestone;

	use yii\data\ActiveDataProvider;


	class MilestoneSearch extends Milestone {


		public $projectName;

		public $countInCompletedTasks;

		public $countCompletedTasks;


		public static function tableName() {


			return "{{%milestone}}";




		public function rules() {


			return [


				[["id", "name", "projectName", "countInCompletedTasks", "countCompletedTasks", "dueDate", "completed"], "safe"],

				["dueDate", "default", "value" => null]






		public function search($params) {


			$query = Milestone::find();





				$query->where(["fkIDWithProjectID" => Yii::$app->user->identity->fkViewingProjectID]);


			$activeDataProvider = new ActiveDataProvider([


				"query" => $query,

				"pagination" => [


					"pageSize" => 20






			$activeDataProvider->sort->attributes["projectName"] = [


				"asc" => ["" => SORT_ASC],

				"desc" => ["" => SORT_DESC]




			$activeDataProvider->sort->attributes["countInCompletedTasks"] = [


				"asc" => [$this->countInCompletedTasksSQL => SORT_ASC],

				"desc" => [$this->countInCompletedTasksSQL => SORT_DESC]




			$activeDataProvider->sort->attributes["countCompletedTasks"] = [


				"asc" => [$this->countCompletedTasksSQL => SORT_ASC],

				"desc" => [$this->countCompletedTasksSQL => SORT_DESC]




			if(!$this->load($params) || !$this->validate())

				return $activeDataProvider;


			$query->andFilterWhere(["like", "", $this->id])

				   ->andFilterWhere(["like", "", $this->name])

				   ->andFilterWhere(["like", "", $this->projectName])

				   ->andFilterWhere(["like", $this->countInCompletedTasksSQL, $this->countInCompletedTasks])

				   ->andFilterWhere(["like", $this->countCompletedTasksSQL, $this->countCompletedTasks])

				   ->andFilterWhere(["like", "milestone.dueDate", !empty($this->dueDate) ? Yii::$app->formatter->asDate($this->dueDate, "Y-MM-dd") : null])

				   ->andFilterWhere(["like", "milestone.completed", $this->completed]);


			return $activeDataProvider;




		// sql

		public function getCountInCompletedTasksSQL() {


			return "(select count(*) from task where task.fkIDWithMilestoneID = and completed = 0)";




		public function getCountCompletedTasksSQL() {


			return "(select count(*) from task where task.fkIDWithMilestoneID = and completed = 1)";







  1. "countCompletedTasks" is set as public property at top of search model.

  2. "countCompletedTasks" is added to rules.

  3. Created the "sort" array for "countCompletedTasks" property.

  4. Added "countCompletedTasks" property to the "andFilterWhere" condition. The SQL for you needs to select the date from the DB in the same format as the date in the magic property.